无法解析有效的JSON字符串

时间:2015-12-05 00:22:15

标签: java php android json arraylist

我正在尝试解析一个JSON字符串并将其映射到一个hashmap,我有一个来自服务器的有效JSONString但是当我遍历它时,我所能获得的只是第一个结果。

 JSONArray peoples = null;
 ArrayList<HashMap<String, String>> personList = new ArrayList<HashMap<String,String>>();

JSONObject jsonObj = new JSONObject(value);
                Log.d("Jello",jsonObj.toString());
                peoples = jsonObj.getJSONArray("product");//check here
                Log.d("Jello",peoples.toString());


                for(int i=0;i<peoples.length();i++){
                    JSONObject c = peoples.getJSONObject(i);
                    String service_group = c.getString("sgroup");
                    String service = c.getString("service");
                    String value = c.getString("value");
                    String updated_at = c.getString("updated_at");

                    HashMap<String,String> persons = new HashMap<String,String>();

                    persons.put("service_group",service_group);
                    persons.put("service",service);
                    persons.put("value",value);
                    persons.put("updated_at",updated_at);


                    personList.add(persons);
                }

我的JSON字符串是:

{"product":[{"sgroup":"Dummy_BIG_ONE","service":"Dummy_UNDER_BIG_ONE","code":"128","value":"0","updated_at":"2015-12-04 21:21:00"}]}{"product":[{"sgroup":"Hello Monkey","service":"Do u work","code":"123","value":"0","updated_at":"2015-12-04 21:27:51"}]}{"product":[{"sgroup":"Checking from Android Device","service":"Monkey","code":"12345","value":"0","updated_at":"2015-12-04 22:55:39"}]}{"product":[{"sgroup":"Checking from Android Device","service":"Monkey","code":"12345","value":"0","updated_at":"2015-12-04 22:55:40"}]}{"product":[{"sgroup":"Checking from Android Device","service":"Monkey","code":"12345","value":"0","updated_at":"2015-12-04 22:55:42"}]}{"product":[{"sgroup":"Hello World","service":"Donkey","code":"24411","value":"0","updated_at":"2015-12-04 22:57:05"}]}{"product":[{"sgroup":"lkfnhjdiofho","service":"dfjdifj","code":"1101","value":"0","updated_at":"2015-12-05 01:15:49"}]}{"product":[{"sgroup":"Baal","service":"Saal","code":"1234","value":"21","updated_at":"2015-12-05 01:34:59"}]}{"product":[{"sgroup":"Inis","service":"Mona","code":"1234","value":"1001","updated_at":"2015-12-05 01:39:51"}]}{"product":[{"sgroup":"Medical Treatment Loan","service":"Number of referral slip","code":"128","value":"0","updated_at":"2015-12-05 01:50:42"}]}{"product":[{"sgroup":"Medical Treatment Loan","service":"Number of referral slip","code":"128","value":"0","updated_at":"2015-12-05 01:55:12"}]}{"product":[{"sgroup":"Medical Treatment Loan","service":"Number of referral slip","code":"128","value":"1000","updated_at":"2015-12-05 01:56:10"}]}

HERE

这是我发送JSON的方式

while($row = mysql_fetch_assoc($output))
        {
            $product = array();
            $product["sgroup"] = $row["service_group"];
            $product["service"] = $row["service"];
            $product["code"] = $row["code"];
            $product["value"] = $row["amount"];
            $product["updated_at"] = $row["updated_at"];

            // user node
            $response["product"] = array();

            array_push($response["product"], $product);

            // echoing JSON response
            echo json_encode($response);
        }

我想在我的ArrayList中添加JSON中的所有数据,以便我以后可以使用它。 谢谢你的帮助。

2 个答案:

答案 0 :(得分:2)

首先,有online tools可用于确定您的JSON是否有效。

以下是格式化JSON数据的更好方法的一般示例:

<?php 
    $bigArray = array();

    for ($x = 0; $x <= 10; $x++) {
            $product = array();
            $product["sgroup"] = $x;
            $product["service"] = $x;
            $product["code"] = $x;
            $product["value"] = $x;
            $product["updated_at"] = $x;

            array_push($bigArray, $product);
        }
      // echoing JSON response
      echo json_encode($bigArray);      
?>

这给出了这个简单且易于解析的有效JSON响应:

[
   {
      "sgroup":0,
      "service":0,
      "code":0,
      "value":0,
      "updated_at":0
   },
   {
      "sgroup":1,
      "service":1,
      "code":1,
      "value":1,
      "updated_at":1
   },
   {
      "sgroup":2,
      "service":2,
      "code":2,
      "value":2,
      "updated_at":2
   },
   {
      "sgroup":3,
      "service":3,
      "code":3,
      "value":3,
      "updated_at":3
   },
   {
      "sgroup":4,
      "service":4,
      "code":4,
      "value":4,
      "updated_at":4
   },
   {
      "sgroup":5,
      "service":5,
      "code":5,
      "value":5,
      "updated_at":5
   },
   {
      "sgroup":6,
      "service":6,
      "code":6,
      "value":6,
      "updated_at":6
   },
   {
      "sgroup":7,
      "service":7,
      "code":7,
      "value":7,
      "updated_at":7
   },
   {
      "sgroup":8,
      "service":8,
      "code":8,
      "value":8,
      "updated_at":8
   },
   {
      "sgroup":9,
      "service":9,
      "code":9,
      "value":9,
      "updated_at":9
   },
   {
      "sgroup":10,
      "service":10,
      "code":10,
      "value":10,
      "updated_at":10
   }
]

至于解析,使用HashMap不是最好的方法。创建Person POJO对象的列表。

首先定义Person类:

class Person {
    public String group;
    public String service;
    public String value;
    public String updated;
    public Person(String g, String s, String v, String u) {
        group = g;
        service = s;
        value = v;
        updated = u;
    }
}

然后,解析非常简单:

    List<Person> personList = new ArrayList<>();
    JSONArray jsonArr;
    try {
        jsonArr = new JSONArray(response);
        for(int i=0;i<jsonArr.length();i++){
            JSONObject c = jsonArr.getJSONObject(i);
            String service_group = c.getString("sgroup");
            String service = c.getString("service");
            String value = c.getString("value");
            String updated_at = c.getString("updated_at");

            Person p = new Person(service_group, service, value, updated_at);

            personList.add(p);
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

答案 1 :(得分:0)

你应该把你的产品放到json数组中:

[
  {
    "product": {
      "sgroup": "lkfnhjdiofho",
      "service": "dfjdifj",
      "code": "1101",
      "value": "0",
      "updated_at": "2015-12-05 01:15:49"
    }
  },
  {
    "product": {
      "sgroup": "Baal",
      "service": "Saal",
      "code": "1234",
      "value": "21",
      "updated_at": "2015-12-05 01:34:59"
    }
  },
  {
    "product": {
      "sgroup": "Inis",
      "service": "Mona",
      "code": "1234",
      "value": "1001",
      "updated_at": "2015-12-05 01:39:51"
    }
  },
  {
    "product": {
      "sgroup": "Medical Treatment Loan",
      "service": "Number of referral slip",
      "code": "128",
      "value": "0",
      "updated_at": "2015-12-05 01:50:42"
    }
  },
  {
    "product": {
      "sgroup": "Medical Treatment Loan",
      "service": "Number of referral slip",
      "code": "128",
      "value": "0",
      "updated_at": "2015-12-05 01:55:12"
    }
  },
  {
    "product": {
      "sgroup": "Medical Treatment Loan",
      "service": "Number of referral slip",
      "code": "128",
      "value": "1000",
      "updated_at": "2015-12-05 01:56:10"
    }
  }
]

你必须稍微改变你的解析器:

JSONArray jsonArray = new JSONArray(string);

    for(int i=0;i<jsonArray.length();i++){
        JSONObject c = jsonArray.getJSONObject(i).getJSONObject("product");
        String service_group = c.getString("sgroup");
        String service = c.getString("service");
        String value = c.getString("value");
        String updated_at = c.getString("updated_at");

        HashMap<String,String> persons = new HashMap<String,String>();

        persons.put("service_group",service_group);
        persons.put("service",service);
        persons.put("value",value);
        persons.put("updated_at",updated_at);


        personList.add(persons);
    }

您不需要将对象放入“product”属性,而是直接放入数组:

[
  {
    "sgroup": "lkfnhjdiofho",
    "service": "dfjdifj",
    "code": "1101",
    "value": "0",
    "updated_at": "2015-12-05 01:15:49"
  },
  {
    "sgroup": "Baal",
    "service": "Saal",
    "code": "1234",
    "value": "21",
    "updated_at": "2015-12-05 01:34:59"
  },