0

I have a string like this

[{"id":"id","name":"","age":"","desig":"salary","dept":""}],
[{"id":"id","name":"","age":"","desig":"salary","dept":""}],[],[],
[],,,,,,ABC,ABC,XYZ,false,Hello

I want to convert this into JSONArray. When I tried to convert I am getting only the value

{"id":"id","name":"","age":"","desig":"salary","dept":""}

in that array. I am not getting the other values.

5
  • 3
    What's the language used? Commented Apr 30, 2014 at 10:11
  • What JSON Library/Framework do you use? Commented Apr 30, 2014 at 10:23
  • I use net.sf.json.JSONArray Commented Apr 30, 2014 at 10:25
  • propably you need an "enclosing" array (an array of arrays) [[{"id":"id","name":"","age":"","desig":"salary","dept":""}],...,Hello] Commented Apr 30, 2014 at 10:25
  • @PeterMmm Tried it but doesn't work out. Getting an exception while converting to JSONArray. The exception is An exception occurred: net.sf.json.JSONException. Commented Apr 30, 2014 at 10:43

3 Answers 3

1

The data you have is not valid JSON, as it's in this format:

x,y,z

... even though x, y and z are all JSON arrays, the actual string itself is not a JSON array. JSONArray parses no further than it needs to, to read what it deems valid JSON, and stops parsing after that.

You need to put the data in valid JSON format:

[x,y,z]

then you can parse it with JSONArray. If your input is always this invalid JSON, you could correct it like so:

JSONArray ja = (JSONArray) JSONSerializer.toJSON("[" + badJsonString + "]");

EDIT: you're using a 4-year old, no-longer-supported fork of the original json.org Java library rather than the supported, up to date, original json.org Java library, so I've changed the code snippet to match.

EDIT: looking at your data, it's not valid JSON data even after the arrays. You have:

valid JSON array,
valid JSON array,empty valid JSON array,,empty valid JSON array,
empty valid JSON array,no data,no data,no data,no data,no data,text without quotes,text without quotes,text without quotes,text without quotes

This couldn't be parsed by any JSON parser. If your data is really in this format, you need to combine a JSON parser with your own parser, e.g.

 while (stillDataToRead) {
     if (nextChar == '[') {
         parseJSONArrayAndAdvanceTheCursor();
         ignoreCommaAndAdvanceTheCursor();
     }
     else if (nextChar == ',') {
         recordABlankField();
     }
     else {
        readAnUnquotedStringUpToTheNextComma();
     }
}

Alternatively... get that data in proper JSON format!

[[{"id":"id","name":"","age":"","desig":"salary","dept":""}],
 [{"id":"id","name":"","age":"","desig":"salary","dept":""}],[],[],
 [],null,null,null,null,null,"ABC","ABC","XYZ",false,"Hello"]
Sign up to request clarification or add additional context in comments.

3 Comments

Getting an error The constructor JSONArray(String) is undefined.
Then you're using an out of date version of the org.json package. The current version is here: github.com/douglascrockford/JSON-java
Ah, I see... net.sf.json (json-lib.sourceforge.net) is a dead fork of the more common json.org code. net.sf.json-lib hasn't been updated for 4 years, and it has deliberately removed the JSONArray(String) constructor despite keeping the API documentation saying it should have it: json-lib.sourceforge.net/apidocs/jdk15/net/sf/json/… says "The constructor can convert a JSON text into a Java object.", but the rest of the Javadoc says there's no such constructor. My advice would be to ditch this old cloned library and use the real one.
0

your String ist not a valid JSONArray. Reffering to json.org a JSONArray start with a [ and ends with a ] like your first line does. So the parser will see the first line as an array and stops parsing afer the first ].

If you want to use your full String as an arry enclise it with [ and ] like this and try again:

[[{"id":"id","name":"","age":"","desig":"salary","dept":""}],
 [{"id":"id","name":"","age":"","desig":"salary","dept":""}],[],[],
 [],,,,,,ABC,ABC,XYZ,false,Hello]

3 Comments

Tried it but doesn't work out. Getting an exception while converting to JSONArray. The exception is An exception occurred: net.sf.json.JSONException.
Anybody tell me atleast how can I store {"id":"id","name":"","age":"","desig":"salary","dept":""} in string1, {"id":"id","name":"","age":"","desig":"salary","dept":""} in string2, ... , ABC in string9, ABC in string10, XYZ in string11, etc.
See the end of my answer: [[{"id":"id","name":"","age":"","desig":"salary","dept":""}], [{"id":"id","name":"","age":"","desig":"salary","dept":""}],[],[], [],null,null,null,null,null,"ABC","ABC","XYZ",false,"Hello"]
0
JsonFactory factory = new JsonFactory();
        JsonGenerator generator;        
//      Create a StringWriter instance to buffer the JSON data.
        Writer outWriter = new StringWriter();  
        try{        
//          Create a JSON generator backed by the StringWriter instance created above.
            generator = factory.createGenerator(outWriter);

            generator.writeStartObject();
                  generator.writeStartArray();
            generator.writeStringField("id","id");
            generator.writeStringField("name", "");

            generator.writeStringField("age", "age");


            generator.writeStringField("desig","salary");
            generator.writeStringField("dept","");            
//          end writting }
            generator.writeEndArray();
            generator.writeEndObject();

            generator.close();


//          System.out.println(outWriter.toString());
            }catch(JsonGenerationException e){
                    e.printStackTrace();
                    }
                    catch (IOException e) {
                        e.printStackTrace();
                        }
        System.out.println(outWriter.toString());

try this to get array & depend upon your requirement change & use it for this you have to use Jackson library

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.