0

I am trying to convert a json string to JSONArray of org.json.JSONArray class, the string contains a dateformat with forward slah, but I am getting the following exception because of the slash included in string.

public static void main(String args[]) throws JSONException{

        String jsonString = "[{ID:1, Name:Ann, DOB:14/08/1991}, {ID:2, Name:Vann, DOB:14/08/1992}]";
        JSONArray jsonArray = new JSONArray(jsonString);
        System.out.println(jsonArray.toString());
    }

Exception in thread "main" org.json.JSONException: Expected a ',' or '}' at 25 [character 26 line 1]
    at org.json.JSONTokener.syntaxError(JSONTokener.java:451)
    at org.json.JSONObject.<init>(JSONObject.java:230)
    at org.json.JSONTokener.nextValue(JSONTokener.java:380)
    at org.json.JSONArray.<init>(JSONArray.java:118)
    at org.json.JSONArray.<init>(JSONArray.java:147)
    at com.s4m.sftp.service.impl.SFTPServiceImpl.main(SFTPServiceImpl.java:1150)
  • 1
    Your JSON is wrong – soorapadman Jan 30 '19 at 08:45
  • 1
    As mentioned, the error is not due to the slash but an invalid json string, is that really the string that causes the error? – Joakim Danielson Jan 30 '19 at 08:56
  • 1
    JSON is wrong, try with what David has mentioned above. – Rax Jan 30 '19 at 09:01
  • If I am using this json string [{ID:1, Name:Ann, DOB:14-08-1991}, {ID:2, Name:Vann, DOB:14-08-1992}] then it's working fine. The issue is because of the slashes. – theNoobProgrammer Jan 30 '19 at 09:02
  • Actually [{'ID':1, 'Name':'Ann', 'DOB':'14/08/1991'}, {'ID':2, 'Name':'Vann', 'DOB':'14/08/1992'}] this is working fine I was not using the quotes probably that was the issue. – theNoobProgrammer Jan 30 '19 at 09:03
  • Character `26` **is** the `/`. Quoting the props and values like @DavidPérezCabrera suggested seems to be worth a try. Otherwise the JSON looks good to me for representing a JSON array. It's not a valid JSON Object, but it **is** an array! – bkis Jan 30 '19 at 09:03
  • 1
    Just to be sure: the version proposed by @DavidPérezCabrera is also not valid JSON. This is the correct JSON representation of your string in Java: `String json = "[{\"ID\":1,\"Name\":\"Ann\",\"DOB\":\"14/08/1991\"}, {\"ID\":2,\"Name\":\"Vann\",\"DOB\":\"14/08/1992\"}]";` – Robby Cornelissen Jan 30 '19 at 09:10
  • @RobbyCornelissen You are right – David Pérez Cabrera Jan 30 '19 at 09:15

2 Answers2

2

String values in JSON

Strings should be quoted... including the attribute names. See the JSON specification:

An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string.

The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks.

Date values in JSON

JSON has no representation for date type. It must be represented as a string. See this answer for more information.

Online JSON validator

You can use online JSON validators to check the validity.

Valid JSON

[
   {
      "ID":1,
      "Name":"Ann",
      "DOB":"14/08/1991"
   },
   {
      "ID":2,
      "Name":"Vann",
      "DOB":"14/08/1992"
   }
]

However, I would use the format yyyy-mm-dd for date values instead of dd/mm/yyyy.

Valid JSON as Java String

String jsonString =
"[{\"ID\":1,\"Name\":\"Ann\",\"DOB\":\"14/08/1991\"},{\"ID\":2,\"Name\":\"Vann\",\"DOB\":\"14/08/1992\"}]";
Community
  • 1
  • 1
haba713
  • 2,465
  • 1
  • 24
  • 45
1

Strings should be wrapped in double quotes.

try with the below JSON string

[{"ID":1, "Name":"Ann", "DOB":"14/08/1991"}, {"ID":2, "Name":"Vann", "DOB":"14/08/1992"}]

Maintain the type as string for all fields . Use toString to convert or just send an RFC 1123 date string ToString("r") to parse

Neethi P
  • 9
  • 4