-1

Well I'm new to Android. I'm getting a JSON string from a remote URL.

[{"key":"myString1","val":"myValue1"},{"key":"myString2","val":"myValue2"},{"key":"myString3","val":"myValue3"},{"key":"myString4","val":"myValue4"},{"key":"myString5","val":"myValue5"}]

I just need to parse this JSON string & display all key-val pair. I tried something like below from one of the tutorial.

        JSONArray jArray = new JSONArray(str);

        json = jArray.getJSONObject(0); //This will take first pair.

But I don't know the syntax for iterating through whole json object. Any help would be appreciated. Thanks in Advance.

1

1 Answer 1

1

There's nothing special in it. You do it like iterating any other array. Let's say you have two String arrays to be filled with values: String[] mKey, mValue

Reading from JSON array will be like:

for (int i = 0; i < array.length(); i++) {
    JSONObject object = array.getJSONObject(i);
    mKey[i] = object.getString("key");
    mValue[i] = object.getString("val");
}
Sign up to request clarification or add additional context in comments.

2 Comments

How to assign assign those value to TextView defined already? Can I assign values within loop to TextView ? Sorry, I'm newbie in Android Programming.
myTextView.setText(); better set TetView text outside the loop. You can use StringBuilder to generate a string with key-value pairs, then set TextView text.

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.