0

This question is related with my previous question

I can successfully get the String in json format from the URL to my spring controller

Now I have to decode it

so I did like the following

@RequestMapping("/saveName")
@ResponseBody
public String saveName(String acc)
{jsonObject = new JSONObject();
    try
    {
   System.out.println(acc);
    org.json.JSONObject convertJSON=new org.json.JSONObject(acc);

    org.json.JSONObject  newJSON = convertJSON.getJSONObject("nameservice");
    System.out.println(newJSON.toString());
    convertJSON = new org.json.JSONObject(newJSON.toString());
    System.out.println(jsonObject.getString("id"));

    }
    catch(Exception e)
    {
        e.printStackTrace();jsonObject.accumulate("result", "Error Occured ");
    }
    return jsonObject.toString();
}

This is the JSON String { "nameservice": [ { "id": 7413, "name": "ask" }, { "id": 7414, "name": "josn" }, { "id": 7415, "name": "john" }, { "id": 7418, "name": "RjhjhjR" } ] }

When I run the code then I get the error

org.json.JSONException: JSONObject["nameservice"] is not a JSONObject.

What wrong I am doing?

Community
  • 1
  • 1
rocking
  • 4,729
  • 9
  • 30
  • 45
  • 1
    nameservice is JSONArray [ ] indicates JSONArray – J.K Dec 20 '14 at 09:45
  • Is there a reason you prefer working with the entire string and then writing parsing logic? Perhaps you should read http://codetutr.com/2013/04/09/spring-mvc-easy-rest-based-json-services-with-responsebody/ – hofan41 Dec 20 '14 at 09:47
  • @hofan41 Thanks for suggesting,Android guys will send me data using this format and I have store id and name in the DB.So I am converting the JSON String to JSON Object and then decoding it.If you have other ways then Please post as an answer – rocking Dec 20 '14 at 09:50

3 Answers3

2

It's not a JSONObject, it's a JSONArray

From your question:

{ "nameservice": [ { "id": 7413, "name": "ask" }, { "id": 7414, "name": "josn" }, { "id": 7415, "name": "john" }, { "id": 7418, "name": "RjhjhjR" } ] }

The [ after the nameservice key tells you it's an array. It'd need to be a { to indicate an object, but it isn't

So, change your code to use it as a JSONArray, then iterate over the contents of that to get the JSONObjects inside it, eg

JSONArray nameservice = convertJSON.getJSONArray("nameservice");
for (int i=0; i<nameservice.length(); i++) {
   JSONObject details = nameservice.getJSONObject(i);
   // process the object here, eg
   System.out.println("ID is " + details.get("id"));
   System.out.println("Name is " + details.get("name"));
}

See the JSONArray javadocs for more details

Gagravarr
  • 47,320
  • 10
  • 111
  • 156
  • Thanks for the answer,Can you please tell me how will I get the name and ids? – rocking Dec 20 '14 at 09:47
  • You need to iterate over the array, fetching out the JSONObjects from that. See edit – Gagravarr Dec 20 '14 at 09:49
  • If I were given `[ { "id": 7413, "name": "ask" }, { "id": 7414, "name": "josn" }, { "id": 7415, "name": "john" }, { "id": 7418, "name": "RjhjhjR" } ]` then How to do.Please tell me – rocking Dec 20 '14 at 10:02
1

It seems you're trying to get a JSONObject when "nameservice" is an array of JSONObjects and not an object itself. You should try this:

JSONObject json    = new JSONObject(acc);
JSONArray  jsonarr = json.getJSONArray("nameservice");

for (int i = 0; i < jsonarr.length(); i++) {
  JSONObject nameservice = jsonarr.getJSONObject(i);
  String id   = nameservice.getString("id");
  String name = nameservice.getString("name");
}
John Cipponeri
  • 882
  • 8
  • 12
1

I don't understand why you do it manualy if you already have Spring Framework.

Take a look at MappingJackson2HttpMessageConverter and configure your ServletDispatcher accordingly. Spring will automatically convert your objects to JSON string and vice versa.

After that your controller method will be looked like:

@RequestMapping("/saveName")
@ResponseBody
public Object saveName(@RequestBody SomeObject obj) {
    SomeObject newObj = doSomething(obj);
    return newObj;
}
Pianov
  • 1,533
  • 9
  • 16
  • sorry I did not understand how to do – rocking Dec 20 '14 at 10:10
  • Try to investigate this example, because it's the common approach for your issue: http://www.journaldev.com/2552/spring-restful-web-service-example-with-json-jackson-and-client-program – Pianov Dec 20 '14 at 10:14