0

I am stuck with code actually i am using spring MVC 4. I have one condition where i need to pass the json object at controller side an iterate it. My Json object look like below

{"comptocome":[
    {"1":"Key Parameters","2":"Cellular Limited","3":"limited","4":"Cellular Limited"},
    {"1":"- Long term","2":"Reaffirmed","3":"football","4":"golf"}
    ]
}

with respect to above i have pass this to controller and iterate according to the number of row for example from above two times loop and also to fetch data as per key can any one help me out sort this problem with help of import org.json.simple.JSONObject package.

Thanks in advance.

5
  • 1
    look at this answer: stackoverflow.com/a/26719883/1614378 Commented Nov 18, 2014 at 14:58
  • You can use Jackson to map object. Possible Answer here stackoverflow.com/questions/6019562/… Commented Nov 18, 2014 at 15:00
  • possible duplicate of How to parse JSON in Java Commented Nov 18, 2014 at 15:41
  • Thanks everyone for your replay i have gone through but did not find any appropriate solution. will any one please elaborate here. Commented Nov 19, 2014 at 6:57
  • Actually i wanna used org.json.simple.JSONArray, org.json.simple.JSONObject, org.json.simple.parser.JSONParser but not org.json one. Commented Nov 19, 2014 at 6:58

2 Answers 2

1

Parse using Jackson JSON

eg:

{
"foo" : ["1","2","3","4"],
"bar" : "xxxx",
"baz" : "yyyy"
}

Could be mapped to this class:

public class Fizzle{
    private List<String> foo;
    private boolean bar;
    private int baz;
    // getters and setters omitted
}

Now if you have a Controller method like this:

@RequestMapping("somepath")
@ResponseBody
public Fozzle doSomeThing(@RequestBody Fizzle input){
    return new Fozzle(input);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reply, is that really compulsion to have a bean class i think without we can achieve.
0

You can use com.google.gson for it:

@RequestMapping(value = "/request", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseClass addDeparturePoint(@RequestBody String request) {
    Gson gson = new Gson();
    RequestClass request = gson.fromJson(request, RequestClass.class);
    ResponseClass response = buildResponce(request);
    return response;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.