Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Parse a JSON Object In Android

Tags:

json

android

I am having some problems pulling values from a JSON object. Here is my code

try {
    JSONObject json = new JSONObject(result);
    JSONObject json2 = json.getJSONObject("results");
    test = json2.getString("name");     
} catch (JSONException e) {
    e.printStackTrace();
}

test is declared as a String. When the code runs it is showing null. If I hover over json2 in debug mode I can see all the values and names within the object.

I also tried

test = json2.length();

This returned test = 0. Even when I hover over the json2 object I can read the values within the object.

Here is an example of a JSON string I will use.

{
    "caller":"getPoiById",
    "results":
    {
        "indexForPhone":0,
        "indexForEmail":"NULL",
        "indexForHomePage":"NULL",
        "indexForComment":"NULL",
        "phone":"05137-930 68",
        "cleanPhone":"0513793068",
        "internetAccess":"2",
        "overnightStay":"2",
        "wasteDisposal":"2",
        "toilet":"2",
        "electricity":"2",
        "cran":"2",
        "slipway":"2",
        "camping":"2",
        "freshWater":"2",
        "fieldNamesWithValue":["phone"],
        "fieldNameTranslations": ["Telefon"],
        "id":"1470",
        "name":"Marina Rasche Werft GmbH & Co. KG",
        "latitude":"52.3956107286487",
        "longitude":"9.56583023071289"
    }
}
like image 854
jiduvah Avatar asked Apr 06 '11 12:04

jiduvah


People also ask

What is JSON parsing in Android?

Android JSON Parser Tutorial JSON (Javascript Object Notation) is a programming language . It is minimal, textual, and a subset of JavaScript. It is an alternative to XML. Android provides support to parse the JSON object and array.

What is JSON object in Android?

JSON stands for JavaScript Object Notation.It is an independent data exchange format and is the best alternative for XML. This chapter explains how to parse the JSON file and extract necessary information from it. Android provides four different classes to manipulate JSON data.

What is data parsing in Android?

What is Parse? Parse is an open-source Android SDK and back-end solution that enables developers to build mobile apps with shared data quickly and without writing any back-end code or custom APIs. Parse is a Node.

What is JSON parsing?

JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON. parse() , as the Javascript standard specifies.


4 Answers

In the end I solved it by using JSONObject.get rather than JSONObject.getString and then cast test to a String.

private void saveData(String result) {
    try {
        JSONObject json= (JSONObject) new JSONTokener(result).nextValue();
        JSONObject json2 = json.getJSONObject("results");
        test = (String) json2.get("name");
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
like image 94
jiduvah Avatar answered Sep 25 '22 10:09

jiduvah


In your JSON format, it do not have starting JSON object

Like :

{
    "info" :       <!-- this is starting JSON object -->
        {
        "caller":"getPoiById",
        "results":
        {
            "indexForPhone":0,
            "indexForEmail":"NULL",
            .
            .
         }
    }
}

Above Json starts with info as JSON object. So while executing :

JSONObject json = new JSONObject(result);    // create JSON obj from string
JSONObject json2 = json.getJSONObject("info");    // this will return correct

Now, we can access result field :

JSONObject jsonResult = json2.getJSONObject("results");
test = json2.getString("name"); // returns "Marina Rasche Werft GmbH & Co. KG"

I think this was missing and so the problem was solved while we use JSONTokener like answer of yours.

Your answer is very fine. Just i think i add this information so i answered

Thank you

like image 34
Kushal Avatar answered Sep 24 '22 10:09

Kushal


Take a look at http://developer.android.com/reference/org/json/JSONTokener.html

This might fix your issue.

like image 10
Dave G Avatar answered Sep 22 '22 10:09

Dave G


JSONArray jsonArray = new JSONArray(yourJsonString);

for (int i = 0; i < jsonArray.length(); i++) {
     JSONObject obj1 = jsonArray.getJSONObject(i);
     JSONArray results = patient.getJSONArray("results");
     String indexForPhone =  patientProfile.getJSONObject(0).getString("indexForPhone"));
}

Change to JSONArray, then convert to JSONObject.

like image 6
wzhjay Avatar answered Sep 23 '22 10:09

wzhjay