Sean
Sean

Reputation: 963

How to parse JSON from localhost?

I'm trying to parse data from my local host which is [{"data":{"statues":"2"}}].

This is my code for JSON:

JSONArray jArray = new JSONArray(result);
             for(int i=0;i<jArray.length();i++){
                     JSONObject json_data = jArray.getJSONObject(i);
                     Log.i("log_tag","statues: "+json_data.getString("statues"));
                     //Get an output to the screen
                    returnString = json_data.getString("statues");
                     tv.setText(returnString);

Everything sounds ok, but in my log-cat, this error occurs:

11-15 09:07:27.403: E/log_tag(3037): Error in http connection!!org.json.JSONException: Value [{"data":{"statues":"2"}}] of type org.json.JSONArray cannot be converted to JSONObject

I say it sounds ok because it catches the statues, but not its value (which is 2). I also tried json_data.getInt("statues"); , but the problem is still the same.

What should I do?!

Upvotes: 0

Views: 1264

Answers (3)

krishna
krishna

Reputation: 4099

you have to get JSONObject like this

for(int i=0;i<jArray.length();i++){
   JSONObject c = jArray.getJSONObject(i);

    // Storing each json item in variable
    String id = c.getString("identifier1");
    String name = c.getString("identifier2");

}

for more details you can use this link

Upvotes: 0

Andreiyfrag
Andreiyfrag

Reputation: 44

Try using JsonConvert.DeserializeObject<>();

Take a look at this: How can I parse JSON with C#?

Upvotes: 0

Hariharan
Hariharan

Reputation: 24853

Try this..

for(int i=0;i<jArray.length();i++){
                     JSONObject json_data = jArray.getJSONObject(i);
                     JSONObject c = json_data.getJSONObject("data");
                     Log.i("log_tag","statues: "+c.getString("statues"));
                     //Get an output to the screen
                    returnString = c.getString("statues");
                     tv.setText(returnString);

}

Upvotes: 2

Related Questions