我有值存储在JSON url中并解析json中的值然后在app中显示。我使用Async任务从JSONArray获取值并显示在列表中。当我从服务器获取值时,我得到了错误,json值是
[{"name":"John","uuid":"B9407F30-F5F8-466E-AFF9-25556B57FE6D","major_id":"67889","minor_id":"7032","notification":"Welcome","type":"Website","website":"estimote.com"}]
日志:
10-16 17:46:38.996: W/System.err(11224): org.json.JSONException: Value [{"notification":"Welcome","uuid":"B9407F30-F5F8-466E-AFF9-25556B57FE6D","type":"Website","website":"estimote.com","major_id":"67889","minor_id":"7032","name":"John"}] at 0 of type org.json.JSONArray cannot be converted to JSONObject
10-16 17:46:38.996: W/System.err(11224): at org.json.JSON.typeMismatch(JSON.java:100)
10-16 17:46:38.996: W/System.err(11224): at org.json.JSONArray.getJSONObject(JSONArray.java:484)
代码:
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
//JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray jarray = new JSONArray(jsonStr);
jObject = jarray.getJSONObject(0);
Log.d("jsonObj_Response: ", "> " + jarray);
// Getting JSON Array node
contacts = jObject.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
//TAG_ID, TAG_NAME, TAG_major_id, TAG_minor_id, TAG_notification, TAG_type
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_major_id);
String address = c.getString(TAG_minor_id);
String gender = c.getString(TAG_notification);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
contact.put(TAG_major_id, email);
contact.put(TAG_minor_id, address);
// adding contact to contact list
contactList.add(contact);
}
答案 0 :(得分:4)
你错过了一个JSONArray
JSONArray jarray = new JSONArray(jsonStr);
for (int i = 0; i < jarray.length(); i++) {
JSONArray innerArray = jarray.optJSONArray(i);
for (int j = 0; j < innerArray.length(); j++) {
jObject = innerArray.getJSONObject(j);
}
}
在OP的编辑
之后 JSONArray jarray = new JSONArray(jsonStr);
for (int i = 0; i < jarray.length(); i++) {
JSONObject jobj = jarray.optJSONObject(i);
if (jobj != null) {
}
}
答案 1 :(得分:0)
jObject = jarray.getJSONObject(0)是一个JSONArray,你的字符串在数组中的数组中有一个对象;)
答案 2 :(得分:0)
你有两个数组(&#34; [[&#34; /&#34;]]&#34;)所以&#34; jObject = jarray.getJSONObject(0);&#34;失败,因为它试图转换&#34; [{&#34; name&#34; ... estimote.com&#34;}]&#34;到JSONObject但这是一个JSONArray。
答案 3 :(得分:0)
jarray [0]是JSONarray而不是JSONObject。
答案 4 :(得分:0)
我的解决方案如下,
JSONArray arr = new JSONArray(jsonStr);//Response string is here
for (int i = 0; i < arr.length(); i++) {
JSONArray arr2 = arr.optJSONArray(i);
for (int j = 0; j < arr2.length(); j++) {
jObject = arr2.getJSONObject(0);
}
}