2015-12-05 70 views
1

我想解析一個JSON字符串並將其映射到一個哈希映射,我有一個有效的JSONString從服務器,但是當我遍歷它時,所有我可以得到它是第一個結果。無法解析一個有效的JSON字符串

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); 
     } 

我想從JSON添加的所有數據的在我的ArrayList中,以便稍後使用它。 謝謝你的幫助。

+3

你** JSON **是無效的。 –

+0

那麼我怎樣才能一個接一個地發送所有的JSON呢?我將它們全部添加到數組中。 –

+0

不要在循環中調用'echo json_encode()',而是在循環後添加一個大數組,然後'echo json_encode()'完整數組。另外請注意,您希望外部元素是JSONArray而不是JSONObject,因此您需要相應地更改解析代碼。 –

回答

2

首先,有online tools可用det呃,如果你的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(); 
    } 
+0

謝謝@Daniel。你是一個拯救生命的人:) 我們成功地在黑客馬拉松中提交了我們的第一個項目:) –

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); 
    } 

你不需要把你的對象變成「產品」屬性,但直接進入陣列:

[ 
    { 
    "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" 
    },