Parsing a json string in java - 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");

Related

Read multipile JSON objects from single file with Jackson

I'm using Jackson 2.6.5.
I'm trying to read the following JSON:
{
"metadata1":"value",
"metadata2":"value"
}
{
"field1":"value",
"field2":"value",
....
}
With the following code:
JSONObject jsonObj = new JSONObject(jsonString);
But jsonObj contains only the "first" part of my JSON (the metadata), How can I read the "second" part of my JSON? (the part with the fields)?
EDIT
I know that my JSON doesn't contain "," so how can I parse it without "," between the jsons?
Seems like the JSON you added is missing a , sign between the two objects.
If you get such an non-json as input string, you might want to consider to:
First manipulate the input string by adding it the missing , . E.g. find the location of }{ and replace it with },{
And only then to insert it to the jackson
Your json is invalid, so you can't parse invalid json scheme by json parsing tools out of the box,
Instead you could read file by yourself into 2 strings - one valid json per string and then parse it with any json parser.

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.

Json parser without json library

I need to make a json parser without json library. The input will be like {:[{}, {}]}. It should return 1 if string is ok and -1 if string is wrong.

Decode json object to string android

I used json_encode(); to convert string to json in php and then response it to android but I can't use the response, how can I convert the json to string?
when I display the response it shows this :
"{\n'OK': \n[\n{\n'Name': 'MyName',\n'Gender':'Male'\n}\n]\n}"
what shall I do?
thank you
Since you're just converting a string to json, you're not returning a JSONObject or JSONArray, according to: http://php.net/manual/en/function.json-decode.php
If you must return a string, you may have to use some json library or write your own parser.
If that doesn't sound appealing, I recommending returning a JSONObject or JSONArray with one element.
For example:
php
echo json_encode( array('result' => 'the string you are encoding') );
java
JSONObject json = new JSONObject( encodedStringResponseFromPhp );
String theStringYouEncoded = (String) json.get( "result" );
You'll need to add a throws JSONException to the function you add this java code too or put it inside a try catch block.
Have you tried using a JSON-Library like https://code.google.com/p/json-simple/? Looks like you need some help decoding the string.
Edit: You should use the json2.js library from Douglas Crockford. It provides some extra features and better/older browser support.
Read more...

How to decode json object having attribute with multiple values in 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");

Categories