I'm trying to read a JSON array. Here is my code.
JSONArray jArray = new JSONArray(jsonString); System.out.println("*****JARRAY*****"+jArray.length()); for(int i=0;i<jArray.length();i++){ JSONObject json_data = jArray.getJSONObject(i); Log.i("log_tag","_id"+json_data.getInt("account")+ ", mall_name"+json_data.getString("name")+ ", location"+json_data.getString("number")+ ", telephone"+json_data.getString("url")+ ",----"+json_data.getString("balance")+ ",----"+json_data.getString("credit")+ ",----"+json_data.getString("displayName") ); }
And my sample JSON files syntax is as follows,
{ "list": [ { "account": 1, "name": "card", "number": "xxxxx xxxx xxxx 2002", "url": "http://www.google.com", "balance": 1.0, "credit": 1.0, "displayName": "hsbc bank" }, { "account": 2, "name": "card2", "number": "xxxxx xxxx xxxx 3003", "url": "http://www.google.com", "balance": 2.0, "credit": 2.0, "displayName": "nsb bank" } ], "count": 2 }
It has a curly brace in front. When i try to execute this code block it gives an error saying
A JSONArray text must start with '[' at character 1 of....
Has anyone encountered a problem like this? Any help would be greatly appreciated. Please show me a sample code block if can. Thanks in advance.
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.
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.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
What is Parse? Parse is an open-source Android SDK and back-end solution that enables developers to build mobile apps with shared data quickly and without writing any back-end code or custom APIs. Parse is a Node.
A JSON Object starts with a {
and ends with a }
while a JSON Array starts with a [
and ends with a ]
.
In your case, change your code to have a JSONObject
instead.
JSONObject json = new JSONObject(jsonString); JSONArray jArray = json.getJSONArray("list"); System.out.println("*****JARRAY*****" + jArray.length()); for(int i=0; i<jArray.length(); i++){ JSONObject json_data = jArray.getJSONObject(i); Log.i("log_tag", "_id" + json_data.getInt("account") + ", mall_name" + json_data.getString("name") + ", location" + json_data.getString("number") + ", telephone" + json_data.getString("url") + ",----" + json_data.getString("balance") + ",----" + json_data.getString("credit") + ",----" + json_data.getString("displayName") ); }
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