I'm new to JSON and trying this tutorial: http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/#comments
I'm new to JSON, C languages, Java and also Android, but am learning. The tutorial uses what I'm calling a named array, but all of the JSON i'm going to be using in my android project will use simple table rows with no named array. Examples of the JSON i'm using and the earthquake json from the tutorial are below.
The tutorial iterates through the earthquake array and converts into a JAVA hashmap list using the following code:
JSONArray earthquakes = json.getJSONArray("earthquakes"); for(int i=0;i<earthquakes.length();i++){ HashMap<String, String> map = new HashMap<String, String>(); JSONObject e = earthquakes.getJSONObject(i); map.put("id", String.valueOf(i)); map.put("name", "Earthquake name:" + e.getString("eqid")); map.put("magnitude", "Magnitude: " + e.getString("magnitude")); mylist.add(map); }
My question is, how can I use json.getJSONArray("")
if my JSON is just simple as below? I can convert the rest of the code, I just need to know how to load that JSON using the getJSONArray("strJsonArrayName")
if I don't have a strJsonArrayName
.
My JSON (UnNamed Array)
[ { "cnt":1, "name":"American", "pk":7 }, { "cnt":2, "name":"Celebrities", "pk":3 }, { "cnt":1, "name":"Female", "pk":2 }, { "cnt":1, "name":"Language", "pk":8 }, { "cnt":1, "name":"Male", "pk":1 }, { "cnt":1, "name":"Region", "pk":9 } ]
Tutorial's JSON (Named Array)
{ "earthquakes":[ { "eqid":"c0001xgp", "magnitude":8.8, "lng":142.369, "src":"us", "datetime":"2011-03-11 04:46:23", "depth":24.4, "lat":38.322 }, { "eqid":"c000905e", "magnitude":8.6, "lng":93.0632, "src":"us", "datetime":"2012-04-11 06:38:37", "depth":22.9, "lat":2.311 }, { "eqid":"2007hear", "magnitude":8.4, "lng":101.3815, "src":"us", "datetime":"2007-09-12 09:10:26", "depth":30, "lat":-4.5172 }, { "eqid":"c00090da", "magnitude":8.2, "lng":92.4522, "src":"us", "datetime":"2012-04-11 08:43:09", "depth":16.4, "lat":0.7731 }, { "eqid":"2007aqbk", "magnitude":8, "lng":156.9567, "src":"us", "datetime":"2007-04-01 18:39:56", "depth":10, "lat":-8.4528 }, { "eqid":"2007hec6", "magnitude":7.8, "lng":100.9638, "src":"us", "datetime":"2007-09-12 21:49:01", "depth":10, "lat":-2.5265 }, { "eqid":"a00043nx", "magnitude":7.7, "lng":100.1139, "src":"us", "datetime":"2010-10-25 12:42:22", "depth":20.6, "lat":-3.4841 }, { "eqid":"2010utc5", "magnitude":7.7, "lng":97.1315, "src":"us", "datetime":"2010-04-06 20:15:02", "depth":31, "lat":2.3602 }, { "eqid":"2009mebz", "magnitude":7.6, "lng":99.9606, "src":"us", "datetime":"2009-09-30 08:16:09", "depth":80, "lat":-0.7889 }, { "eqid":"2009kdb2", "magnitude":7.6, "lng":92.9226, "src":"us", "datetime":"2009-08-10 17:55:39", "depth":33.1, "lat":14.0129 } ] }
In the tutorial, based on the answers from @MДΓΓ БДLL and @Cody Caughlan , I was able to reformat the JSONFunctions.getJSONFromURL into a JSONArray instead of a JSONObject. Here is my modified working code, thank you!
public class JSONfunctions { public static JSONArray getJSONfromURL(String url){ InputStream is = null; String result = ""; JSONArray jArray = null; HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); is = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result=sb.toString(); jArray = new JSONArray(result); return jArray; } }
You can easy get a JSON array element by key like this: var value = ArrName['key_1']; //<- ArrName is the name of your array console. log(value);
We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.
You can use the regular length() method. It returns the size of JSONArray. If the array is empty, it will return 0. So, You can check whether it has elements or not.
When you are working with JSON data in Android, you would use JSONArray to parse JSON which starts with the array brackets. Arrays in JSON are used to organize a collection of related items (Which could be JSON objects). Show activity on this post. a JSONObject of {id: 1, name: 'B'} is equal to {name: 'B', id: 1} .
You don't need to call json.getJSONArray()
at all, because the JSON you're working with already is an array. So, don't construct an instance of JSONObject
; use a JSONArray
. This should suffice:
// ... JSONArray json = new JSONArray(result); // ... for(int i=0;i<json.length();i++){ HashMap<String, String> map = new HashMap<String, String>(); JSONObject e = json.getJSONObject(i); map.put("id", String.valueOf(i)); map.put("name", "Earthquake name:" + e.getString("eqid")); map.put("magnitude", "Magnitude: " + e.getString("magnitude")); mylist.add(map); }
You can't use exactly the same methods as in the tutorial, because the JSON you're dealing with needs to be parsed into a JSONArray
at the root, not a JSONObject
.
JSONArray
has a constructor which takes a String
source (presumed to be an array).
So something like this
JSONArray array = new JSONArray(yourJSONArrayAsString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With