How to parse json format String in java [duplicate] - java

This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 8 years ago.
How to parse following json string in java. I would like to retrieve one by one.
{"IN1005*1001302*CWH":[{"qtyreceived":"5","itemcode":"1618306"},{"qtyreceived":"0","itemcode":"1618305"},{"qtyreceived":"0","itemcode":"288242"]}
Output can be
head=IN1005*1001302*CWH
qtyreceived=5
itemcode:1618306
and so on
Please any one help me . I saw many examples but none of them are matching with my requirement. If you found solution for my question in another old post please let me know the post details and url

You could use GSON, check: https://code.google.com/p/google-gson/
Possible duplicate of JSON parsing using Gson for Java

I would like to suggest an algorithm:
1>Use any good REST API for java. I would suggest Apache Jersey.
2>You will have a Resource Layer in your application in which you will recieve this JSON String from the other layer.
3> You can refer to the example as such in http://www.vogella.com/tutorials/REST/article.html
4>Now the method that you are using will recieve the JSON String as argument. That can be consumed using #XMLROOTElement tag.
Note: you need to convert IN1005*1001302*CWH to IN10051001302CWH or something useful as it has special characters and you cannot create a java class.
getJSON(WSObject object){}
Some description of your java object.
#XmlRootElement
WSObject{
List<IN10051001302CWH> jsonObject;
}
And IN10051001302CWH would be again another object having properties that of your JSON properties which have same spelling :-
#XmlRootElement
IN10051001302CWH{
private Long qtyrecieved;
private Long itemcode;
}

You can achieve this using Jackson, see this Link
Here my answer for converting an object into json format.

Related

How to use google.protobuf.struct in proto file to store json result in gRPC java

I Want to Return a JSON response from server to client in gRPC.
one possible way is to convert it to string return the response then convert back to Json Object in client side, but i want to know can we do better?.
i am doing some google and found we can do it with the help of google.protobuf.struct
but didn't actually find any good example.
i want an example how i can use it as JSON in java.
If you are using proto3, one option is to define a protobuf message that mirrors the JSON object you wish to populate. Then you can use JsonFormat to convert between protobuf and JSON.
Using a com.google.protobuf.Struct instead of a self-defined message can also work. There is an example shown in the similar question.

How to convert XML to JSON in Java excluding some tags?

I want to convert XML to JSON in Java but most of the answers focus on converting all tags in XML to JSON. But my requirement is to skip some of the tags and convert remaining tags to JSON. Any help would be appreciable.
I have tried XML.toJSONObject(String); method from the org.json package but it does not suggest any way to skip some tags.
if you are using gson simply using remove api in JsonObject under com.google.gson
lets suppose we have test attribute in your model that you do not want to display,
JsonObject jsonObj;
jsonObj.remove("test");
You will be having the json object without test attribute.

Convert RSS feed to JSON in java

Is there any api or something to convert RSS feed to Json?
I used the api rss2json api with restTemplate, it's working fine when you map it with an entity but this one doesn't support multiple requests as it's gets overloaded plus there is no documentation for it or support so if the api goes down so is my app and I couldn't find something similar besides the rome plugin that converts to an object. I want direct conversion to Json.
I'm not sure what you mean by direct conversion to JSON, but you could simply use org.json to convert XML String to JSON String:
String xml = ..
JSONObject jObject = XML.toJSONObject(xml);
String json = jObject.toString();
More discussion here: Converting xml to json using jackson
I finished by using Rome plugin and built my own JSon structure step by step. no better solution so far.

Parse json received from webservice? [duplicate]

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 8 years ago.
I need to parse this JSON:
{
"out":{
"nroRegistros":1,
"asignaciones":[
{
"lat":"456",
"lng":"456",
"direccion":"Nocedal 108 Estacion Central",
"depto":null,
"descripcion":"Casa amplia, cerca del metro las rejas",
"tipoVehiculo":null,
"referencia":null,
"rutDenunciante":null,
"nombreDenunciante":null,
"apePaternoDenunciante":null,
"apeMaternoDenunciante":null,
"fonoMovilDenunciante":null,
"ambito":null,
"prioridad":null,
"ley":null,
"articulo":null,
"marcaVehiculo":null,
"colorVehiculo":null,
"placaPatente":null,
"id":null
}
]
},
"status":{
"code":1,
"message":"success"
}
}
From all I have read I cant find an example or something to guide me. I am new to json and i can't really find a way to make it work. I have read a lot of tutorials but they all are quite simple. I understand them but I cant make this one work.
First of all, use some json parser to visualize data, for example http://jsonviewer.stack.hu/. This will make it for you a lot easier to understand the structure of the data.
Next step is to create a model class to accept the json you are receiving. This you have to do it by yourself, in eclipse or any other IDE you might be using.
It will look something like this:
public class JsonModel{
public Object out;
public Object status;
}
here I put Object as a general type, you may want to define the variables to an appropriate type to reflect the structure of the json file.
Once you have the model, you can simply get the data by any json library, I like to use Gson 3rd party for any json work. It would be something like so:
string json = getJsonFromInternet();
JsonModel mymodel = new Gson().fromJson(json, JsonModel.class);
your data will be stored in mymodel, as Java object, which you can use according to your needs.
I hope this helps.
find Json parsing tutorials
Json Parsing good example
You can achieve this by using JsonLib.
Try to put those values in HashMap , make sure you create the same structure of pojo classes as defined in the Json String (case sensitive)
Your json will be mapped using the classMap.put method. From there on , we have a great controller over the java bean object.
Try to explore few things, before you jump into it
String json = "{'out':[{'test':'testname'},{'test2':'testname2'}]}";
Map classMap = new HashMap();
classMap.put( "out", YourClass.class );
MyBean bean = JSONObject.toBean( JSONObject.fromObject(json), MyBean.class, classMap );
Reference
<link>http://json-lib.sourceforge.net/snippets.html</link>

How to convert List<Object> to JSON on playframework Java

First of all, I am using playframework 2 with Java.
I have a Bootstrap-lookahead input field in a view and I want to use a json array as source for it, as described here.
I can generate json at server-side with:
Json.toJson(users)
or on client with:
#{Json.toJson(users)}
However it generates strings with " and when I try to create the bootstrap-lookahead field with this data, it gives me
Uncaught SyntaxError: Unexpected token &
Could someone help me with that?
Thank you
You can prevent escaping by using #Html(Json.toJson(users)) in template.
Docs, last paragraph

Categories