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.
3 Answers 3
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"]
3 Comments
The constructor JSONArray(String) is undefined.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.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
JSONArray. The exception is An exception occurred: net.sf.json.JSONException.{"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.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
[[{"id":"id","name":"","age":"","desig":"salary","dept":""}],...,Hello]JSONArray. The exception isAn exception occurred: net.sf.json.JSONException.