Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse JSON Array (Not Json Object) in Android

I have a trouble finding a way how to parse JSONArray. It looks like this:

[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...] 

I know how to parse it if the JSON was written differently (In other words, if I had json object returned instead of an array of objects). But it's all I have and have to go with it.

*EDIT: It is a valid json. I made an iPhone app using this json, now I need to do it for Android and cannot figure it out. There are a lot of examples out there, but they are all JSONObject related. I need something for JSONArray.

Can somebody please give me some hint, or a tutorial or an example?

Much appreciated !

like image 208
SteBra Avatar asked Sep 24 '13 09:09

SteBra


People also ask

How do you parse an array object in JSON?

Use the JSON. parse() method to pase a JSON array, e.g. JSON. parse(arr) . The method parses a JSON string and returns its JavaScript value or object equivalent.

What is JSON object and JSON array in Android?

A JSON data consists of 4 major components that are listed below: Array: A JSONArray is enclosed in square brackets ([). It contains a set of objects. Object: Data enclosed in curly brackets ({) is a single JSONObject. Nested JSONObjects are possible and are very commonly used.

What is JSON array in Android?

JSONArray(String json) Creates a new JSONArray with values from the JSON string. JSONArray(Object array) Creates a new JSONArray with values from the given primitive array.

What is parsing in Android explain JSON parsing with suitable example?

Advertisements. 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.


1 Answers

use the following snippet to parse the JsonArray.

JSONArray jsonarray = new JSONArray(jsonStr); for (int i = 0; i < jsonarray.length(); i++) {     JSONObject jsonobject = jsonarray.getJSONObject(i);     String name = jsonobject.getString("name");     String url = jsonobject.getString("url"); } 
like image 111
Spring Breaker Avatar answered Sep 29 '22 16:09

Spring Breaker