0

I have tried to parse a JSON object by lot of ways but can't figure out how to parse that.

This is my json string:

 {
  "JSONDataResult":

        {"Messages":
                   [{ "Id":"0",
                      "Category":"Sport",
                      "Title":"It's a goal",
                      "Content":"sport content"
                    }]
        }
 }

I tried this:

        JSONArray arr = result.getJSONArray("Messages\\");
        for (int j = 0; i < arr.length(); j++)
        {
            String post_id = arr.getJSONObject(i).getString("post_id");
            Id = result.getString("Id\\");
            Title = result.getString("Title\\");
            Content = result.getString("Content\\");   
        }
3
  • Possible duplicate of How to parse JSON in Java Commented Dec 25, 2015 at 9:03
  • What is the use of these slash \\ ?? Commented Dec 25, 2015 at 9:04
  • Is that a json coming from api or a static string, api wont return '\' in result. Commented Dec 25, 2015 at 9:10

2 Answers 2

1

there must be a lot of answers out there but this is the one i did. guess we have a String coming from server or getting it from android cache where you stored before..

String listdata = "some kind of json data, might be your sample..";
JSONObject jsnobject = new JSONObject(listdata);
System.out.println("jsnobject: " + jsnobject);
return jsnobject.getJSONArray("Messages");

or you already have the reponse as a list structured

JSONArray jsonArray = new JSONArray(response.toString());
for (int i = 0; i < jsonArray.length(); i++) {
       .....
}

First you convert it to a valid json object that a json array. where you want the list starts, Messages etc.. so that you can get each elements id, title .. by getString in you example..

grats

Sign up to request clarification or add additional context in comments.

1 Comment

this way not founding "Messages"
0

Slash is not required. You simply do this

  JSONArray arr = result.getJSONArray("Messages");
    for (int j = 0; i < arr.length(); j++)
    {
        JsonObject object = arr.getJSONObject(i);
       Strng id = object .optString("Id");
      String Title = object .optString("Title");

    }

1 Comment

it fail to find "Messages"

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.