0

I have this JSON file :

[{"Id":"1","name":"Bryan"}]

But I don't know how to achieve this in Android. I've tried this:

    val url = "url of my website"

Then this:

var json=JSONObject(values[1])
                val id = json.getJSONArray("Id")
                val name = json.getJSONObject("name")
                var.text = name

But it does not work. I would like to know where's the error.

1
  • Some more information would be nice. How are you loading / assigning the "values" collection, and what incorrect output do you get? Commented Jun 22, 2017 at 2:28

2 Answers 2

1

You already have your object with you in your json object, id and name are not JSONobjects, they are inside your json object. To retrieve them do this:

var json=JSONObject(values[1])
val id = json.getString("Id")
val name = json.getString("name")
var.text = name
Sign up to request clarification or add additional context in comments.

Comments

0

[{"Id":"1","name":"Bryan"}] this is a json array

so use the following to extract

I assume

String response= "[{"Id":"1","name":"Bryan"}]";

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

  JSONObject object= jarray.getJsonObject(i);
  int id=object.getString("Id");
  String name=object.getString("name);

}

If the array size is more than one , you can initialise an arraylist with some object

Hope it helps you

Comments

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.