How can I set text the array 0 or fname in JSONobject I'm new in using JSON

1232 views java
-3

My JSON file looks like the following.

{
  "data": [{
      "fname": "Ralph",
      "lname": "Rojas",
      "pid": "444"
  }]
}

I've got a little problem with parsing JSON into my android app. This is what I have tried so far.

JSONArray arr;
try {
    arr = new JSONArray(json_string);
    JSONObject jObj = arr.getJSONObject(0);
    fname = jObj.getString("fname");
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
runOnUiThread(new Runnable(){
    public void run() {
       Toast.makeText(getApplicationContext(),fname,Toast.LENGTH_LONG).show();
    }
});

Toast.makeText(getApplicationContext(), fname, Toast.LENGTH_LONG).show();
name = (TextView)findViewById(R.id.name);
name.setText(fname);

answered question

your JSON is not JSONArray. It is a JSONObject with a JSONArray in it. You are trying to create JSONArray from it. You need one more step - to get data JSONArray from the JSONObject

4 Answers

8

Use Gson for easy JSON parsing.

You just have to add the following dependency in your build.gradle file.

implementation 'com.google.code.gson:gson:2.8.5'

And then in your code, create the following classes first.

public class Data {
    public People[] data;
}

public class People {
    public String fname;
    public String lname;
    public String ppid;
}

Now parse the JSON like the following.

Gson gson = new Gson();
Data data = gson.fromJson(yourJSONString);

You will find the values parsed in your data variable.

Hope that helps!

posted this
9

You need to create JSONObject before you retrieve JSONArray from it. Here is example code.

 try {
    JSONObject jsonObject = new JSONObject(json_string);
    arr = jsonObject.getJSONArray("data");
    JSONObject jObj = arr.getJSONObject(0);
    fname = jObj.getString("fname");
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

posted this
6

Try this

JSONObject obj;
    try {
        obj = new JSONObject(json_string);
        JSONArray arr = obj.getJSONArray("data");
        JSONObject obj1 = arr.getJSONObject(0);
        fname = obj1.getString("fname");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    runOnUiThread(new Runnable(){
        public void run() {
           Toast.makeText(getApplicationContext(),fname,Toast.LENGTH_LONG).show();
        }
        });


    Toast.makeText(getApplicationContext(), fname, Toast.LENGTH_LONG).show();
    name = (TextView)findViewById(R.id.name);
    name.setText(fname);

posted this
12

You could do like this:

    JSONArray arr;
    JSONObject obj = null;
    try {
        obj = new JSONObject(json_string);
        arr = obj.getJSONArray("data");//Firstly get outer most json array
        JSONObject rlt = arr.getJSONObject(0);//then get item in json array
        fname = rlt.getString("fname");
    } catch (JSONException e) {
        e.printStackTrace();
    }

posted this

Have an answer?

JD

Please login first before posting an answer.