1

I have created one json array in view; it has dynamic data I am sending that array as hidden input field to the spring controller. I am getting that array as:

String string=request.getParameter("jsonStateCompanyOnSubmit");
logger.info("jsong format is:"+string);

The JSON data is:

[
{
  "MH":{
     "data":{
        "ACC":4,
        "SHREE":2
     },
     "name":"MH"
  },
  "MP":{
     "data":{
        "ACC":9,
        "SHREE":6
     },
     "name":"MP"
  }
}
] 

How can I extract:

MH,ACC,4
MH,SHREE,2  
MP,ACC,9
MP,SHREE,6
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Refer this tutorial parsing JSON using gson library http://www.journaldev.com/2321/google-gson-api-for-json-processing-example-tutorial – Ashok_Pradhan Jun 08 '14 at 09:06

4 Answers4

1

Without going into the details, check the package javax.json. There you can find the Json class to create a parser and then access the data in a similar way as with a map.

Augusto
  • 28,839
  • 5
  • 58
  • 88
0

in addition to what others have wrote - I would recommend to you on Jackson:

List<?> list = new org.codehaus.jackson.map.ObjectMapper().readValue(FileUtils.read(file), List.class);
for(Object obj: list){
    Map<?, ?> map = (Map<?, ?>) obj;
    //do your stuff here
}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Hovav
  • 151
  • 2
  • 13
0

Simply use JSONObject constructor.

A JSONObject constructor can be used to convert an external form JSON text into an internal form whose values can be retrieved with the get and opt methods, or to convert values into a JSON text using the put and toString methods.

Sample code: (Do in the same way for other values)

JSONObject jsonObject = new JSONObject(jsonString);

JSONObject mh = jsonObject.getJSONObject("MH");
int acc=mh.getJSONObject("data").getInt("ACC"); // output 4

Note: Just remove enclosing [] before doing this.

Braj
  • 46,415
  • 5
  • 60
  • 76
0

You need to parse JSON from string. Using JSON.simple API you can use a parser.

JSONParser parser = new JSONParser();
try {
    Object obj = parser.parse(string);
    JSONArray arr = (JSONArray) obj; 
    Iterator<Object> iterator = arr.iterator();
    while (iterator.hasNext()) {
      JSONObject m = (JSONObject)iterator.next();
      JSONObject data = (JSONObject) m.get("data");
      Integer acc = (Integer)data.get("ACC");
      ...  
    }   
} catch (ParseException e) {
  e.printStackTrace();
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • thanks for reply. but in my case ACC is not fixed content if in database changes will occur that name will change. only data and name are fixed in that array – user3719245 Jun 08 '14 at 09:07
  • If the name will change you should provide the name. you can find decoding examples [here](https://code.google.com/p/json-simple/wiki/DecodingExamples). – Roman C Jun 08 '14 at 09:08
  • @user3719245 You can also choose mapping between json object and java see [here](https://code.google.com/p/json-simple/wiki/MappingBetweenJSONAndJavaEntities). – Roman C Jun 08 '14 at 09:18