如何从arraylist中读取android中的这个数组

时间:2016-05-14 06:30:09

标签: java android arrays json

嘿,我有这个数组,我的数组列表正在生成

 {
id= 197, 
image=http://example.com/wp-content/uploads/2016/03/bottle_pg9.png,
     category=, 
title=example Water 320ML Bottle (Pack of 24) For 6 Months, price=238.50
}

但我怎么读这个数组我试过

JSONObject  jsonRootObject = new JSONObject(createArray);
JSONObject data = jsonRootObject.getJSONObject("image");

但它给了我错误

org.json.JSONException: Unterminated object at character 21 of {id= 197, image=http://example.com/wp-content/uploads/2016/03/bottle_pg9.png, category=, title=VOLO example 320ML Bottle (Pack of 24) For 6 Months, price=238.50}

4 个答案:

答案 0 :(得分:2)

只需在XML中添加可用的QUOTE“”标记

<?xml version="1.0" encoding="UTF-8"?> <music> <song> <id>198</id> <title>"Eample Water 320ML Bottle (Pack of 24) For 12 Months"</title> <price>225.20</price> <desc /> <image>"Example.com/wp-content/uploads/2016/03/bottle_pg9.png"
 <category>"test"</category> </song> </music>

然后像这样阅读

JSONObject jsonRootObject = new JSONObject(createArray);

String data = jsonRootObject.getString("id");

System.out.println("The data "+data);

答案 1 :(得分:0)

您的输入字符串不是JSON格式。正确的格式是:

 {
    "id" : 197, 
    "image": "http://example.com/wp-content/uploads/2016/03/bottle_pg9.png",
    "category": "", 
    "title": "Example Water 320ML Bottle (Pack of 24) For 6 Months",
    "price": 238.50
}

答案 2 :(得分:0)

JSON应该如下所示

{id:197,image:'http://example.com/wp-content/uploads/2016/03/bottle_pg9.png',category:'',title:'example Water 320ML Bottle (Pack of 24) For 6 Months',price:238.50}

答案 3 :(得分:0)

您的回答不正确。尝试制作正确的json格式,然后尝试用键读取它会起作用 使用this site

进行json验证