How to decode json object having attribute with multiple values in java? - java

this is my json object in javascript that i want to parse in java
var param{
"attrib1":"abc",
"attrib2":["x","y","z"]
}
I'm using jackson library. I am not able to retrieve the members of attrib2. How to do it? help

To get element from JSON Object you can do this
JSONObject json = (JSONObject)new JSONParser().parse(yourString);
System.out.println("attrib2=" + json.get("attrib2"));

attrib2 is JsonArray so please get jsonArray.
jsonObject.optJSONArray("attrib2");

Related

Treves the JSON object and manipulate the value in java

What im trying to do is
JSON:
{
aKey:{
aChildKey:""
},
bKey:""
}
expected:
aKey:{
aChildKey:"aKey.aChildKey"
},
bKey:"bKey"
}
Please can some one help me in getting the expected the value
You need to deserialize the JSON into an object, set the values, then serialize it back into JSON. There are a number of libraries you can use for this, like org.json, gson, or Jackson. Those libraries also allow you to modify the value directly. For example, using org.json, you can do something like this:
JSONObject jsonObject = new JSONObject(myJsonString);
jsonObject.getJSONObject("akey").put("aChildKey","aKey.aChildKey");
See How to parse JSON in Java

Java JSONObject won't transform arrays into Json?

I'm using this code in Java:
JSONObject jsonObject = new JSONObject(response.body().string());
and it will lead to null when I put strings like
[{...},{...}, ..., {...}]
where {...} is a valid JsonObject.
What should I do? I guess json must begin with { and end with } always. That's because it's null. What should I do? Is there a way to make JSON library deal with this automatically? I can't control the place from where I receive this string of 'json array'.
Because the string you've given isn't an object, but an array.
You'll have to read it like that (using Java EE's JSON libraries):
JsonArray array = jsonReader.readArray();

Java parsing from escaped json string

There are many threads related to this, but I can't solve my issue.
I get this string from parsing an iterable using GSON.
Iterable<ParametrosProveedores> proveedoresList;
proveedoresList = proveedoresRepository.findAll(); //From spring repository
String jsonString = gson.toJson(proveedoresList);
jsonString value is:
[{\"id\":1,\"proveedor\":\"CALIXTA\",\"unaVia\":true,\"dosVias\":true,\"plazasSi\":\"todas\",\"plazasNo\":\"\",\"turnoUnaVia\":false,\"turnoDosVias\":false},{\"id\":2,\"proveedor\":\"MOVILE\",\"unaVia\":true,\"dosVias\":true,\"plazasSi\":\"51,52\",\"plazasNo\":\"\",\"turnoUnaVia\":false,\"turnoDosVias\":false},{\"id\":3,\"proveedor\":\"TWILIO\",\"unaVia\":true,\"dosVias\":true,\"plazasSi\":\"todas\",\"plazasNo\":\"51\",\"turnoUnaVia\":false,\"turnoDosVias\":false},{\"id\":4,\"proveedor\":\"OTRO\",\"unaVia\":true,\"dosVias\":true,\"plazasSi\":\"todas\",\"plazasNo\":\"\",\"turnoUnaVia\":false,\"turnoDosVias\":false}]
Which is a json array. Is there really no way to parse from that string without removing escapes manually?
All I want to do is:
JSONArray jsonArray = parseFrom(jsonString);
Is it possible?
Since you are using a generic in the form of an Iterable<T>, you may need to use:
String jsonString = gson.toJson(proveedoresList, typeOfSrc);
Where typeOfSrc is the type of your proveedoresList. that way gson knows how to serialize the object properly.

Find if a JSONObject contains a particular JSONARray

I am working on a spring MVC application. I am using org.json jar to deal with jsonObjects. I am trying to find if a JSONObject contains a JSON array. Can any one please help me in this:
Say i have a JSON Object:
{"Object":{
"array":["",""]
}
}
So i want to know if array exists in that json object as some times the JSONObject will be :
{"Object":{
"array11":["",""]
}
}
Please help me.
Per the javadocs for the org.json JSON library, JSONObject has an optJSONArray() method that does not throw an exception if the key you are targeting does not exist or the value at that key is not a JSON array. For a JSONObject "foo":
JSONArray array = foo.optJSONArray("array");
if (array != null) {
// do stuff with the array
}
You can use org.json (https://github.com/stleary/JSON-java) to do this. You can simply use this code to create the JSON Object:
JSONObject object = new JSONObject(String);
And then you can use the isNull(String) method to check if a value exists in the object. You can get the array using the getJSONArray(String) method, create a loop (such as a for loop) and loop through the array (which accepts ints instead of Strings).

Parsing a json string in java

I am trying to parse a json string of the format :
{"magha": {"2014-04-04 14:27:08.669217": "125"}}
I am not able to create a parser for the same. Looking forward for any help.
Download the json-lib-jar
Use JSONObject
Do like this
JSONObject jobject=new JSONObject(yourjsonstring);
String magha=jobject.getString("magha");

Categories