【问题标题】:How to read this array in android from arraylist [closed]如何从arraylist中读取android中的这个数组[关闭]
【发布时间】:2016-05-14 06:30:09
【问题描述】:

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

 {
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}

【问题讨论】:

  • 不是字符串格式。请您的后端开发人员以字符串格式提供。
  • 我从我的gridview String createArray = songList.get(position).toString();
  • 我从 gridview 得到这个
  • @muhammadzeeshan - Arrays[ 开始,而不是 {

标签: java android arrays json


【解决方案1】:

只需在您的 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);

【讨论】:

  • 嘿,谢谢它有效,我只是添加了引号并像这样阅读它们,然后它就有效了
【解决方案2】:

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】:

    您的回答不正确。尝试制作正确的 json 格式,然后尝试使用密钥读取它会起作用。
    使用this site进行json验证

    【讨论】:

      【解决方案4】:

      您的输入字符串不是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
      }
      

      【讨论】:

      猜你喜欢
      • 2021-09-15
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      • 2020-03-27
      • 2011-10-14
      • 1970-01-01
      相关资源
      最近更新 更多