How to parse json format String in java [duplicate

2019-09-21 05:21发布

问题:

This question already has an answer here:

  • How to parse JSON in Java 31 answers

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

回答1:

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



回答2:

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;      

}


回答3:

You can achieve this using Jackson, see this Link

Here my answer for converting an object into json format.