2

I have this JSONObject which is returned as response from a 3rd party API.

[
[
    {
        "msg": "hi",
        "uuid": "fc8c5dd3-d46c-4945-894d-6160f830d815"
    },
    {
        "msg": "hihe",
        "uuid": "fc8c5dd3-d46c-4945-894d-6160f830d815"
    }
],
14281343855179004,
14281349424008428
]

How can I parse this?

This is what I get

[[{"msg":"hi","uuid":"fc8c5dd3-d46c-4945-894d-6160f830d815"},{"msg":"hihe","uuid":"fc8c5dd3-d46c-4945-894d-6160f830d815"}],14281343855179005,14281349424008427]

My Code

  try {
                JSONObject reader = new JSONObject(message.toString());

                JSONObject sys  = reader.getJSONObject("uuid");
                String msg = sys.getString("msg");
                System.out.println(msg);



            } catch (JSONException e) {
                e.printStackTrace();
            }
WISHY
  • 11,067
  • 25
  • 105
  • 197
  • 1
    Could you show us your code – Juxhin Apr 04 '15 at 08:13
  • you could use a search engine and look for a java library which will do the job for you. there are several good libraries you could include in your project. – MWiesner Apr 04 '15 at 08:18
  • @Juxhin updated the question please check – WISHY Apr 04 '15 at 08:18
  • Your output doesn't match with code you posted. Your code would generate exceptions since you want to read array as object, or String as object. Remember: arrays start with `[`, objects start with `{`. – Pshemo Apr 04 '15 at 08:29
  • What do think your code should do and why do you think so? – Pshemo Apr 04 '15 at 08:40

3 Answers3

2

Try,

JSONArray json = new JSONArray("[[{\"msg\":\"hi\",\"uuid\":\"fc8c5dd3-d46c-4945-894d-6160f830d815\"},{\"msg\":\"hihe\",\"uuid\":\"fc8c5dd3-d46c-4945-894d-6160f830d815\"}],14281343855179005,14281349424008427]");
JSONArray arr  = json.getJSONArray(0);

for (int i = 0; i < arr.length(); i++){
    String message = arr.getJSONObject(i).getString("msg");
    String uuid    = arr.getJSONObject(i).getString("uuid");
    System.out.println("message : "+message);
    System.out.println("uuid    : "+uuid);
}

Output :

message : hi
uuid    : fc8c5dd3-d46c-4945-894d-6160f830d815
message : hihe
uuid    : fc8c5dd3-d46c-4945-894d-6160f830d815
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
1

You could directly use JSONArray instead of JSONObject

  public static void parseJson(String message) {
    try {
      JSONArray json = new JSONArray(message);    
      JSONArray elemArr = json.getJSONArray(0);

      for (int i = 0; i < elemArr.length(); i++) {
        String msg = elemArr.getJSONObject(i).getString("msg");
        System.out.println("msg=" + msg);
        String uuid = elemArr.getJSONObject(i).getString("uuid");
        System.out.println("uuid=" + uuid);        
      }
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }
Neo
  • 4,640
  • 5
  • 39
  • 53
0

GSON is good example to convert Json to Java objects.

http://howtodoinjava.com/2014/06/17/google-gson-tutorial-convert-java-object-to-from-json/

Thanks, Pavan

Pavan
  • 1,219
  • 13
  • 15
  • [Are answers that just contain links elsewhere really “good answers”?](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers) – Pshemo Apr 04 '15 at 09:40