2013-07-25 368 views
2

我有這樣的JSON響應。我嘗試了很多,但無法解析值。無法解析JSON響應

這一個是我的實際響應...

{"ResponseCode":"000","ResponseDescription":"Successful","SystemServiceID":["0000"],"SystemServiceName":["Test"],"ProductID":["000"],"ProductName":["Test"],"ProductDescription":["Test product"],"MinimumValue":[10000],"MaximumValue":[500000],"ImageURL":[null],"Country":["AAAA"],"CompanyID":["1"],"CompanyName":["Test"],"FieldLevel":["2"],"FieldInfo":["{\"Field1\":{\"Field Name\":\"Phone Number\",\"Field Type\":\"Number\",\"Validation\":{\"Min\":\"4\",\"Max\":\"8\"}},\"Field2\":{\"Field Name\":\"Email\",\"Field Type\":\"String\",\"Validation\":{\"Regular Expression\":\"abcd\",\"Min Length\":\"10\",\"Max Length\":\"20\"}}}"]} 

出於此,我能夠解析所有領域想到下面一個...

{ 
    "Field1": 
    { 
      "Field Name":"Phone Number", 
      "Field Type":"Number", 
      "Validation":{"Min":"4","Max":"8"} 
    }, 

    "Field2": 
    { 
      "Field Name":"Email", 
      "Field Type":"String", 
      "Validation":{"Regular Expression":"abcd","Min Length":"10","Max Length":"20"} 
    } 
} 

我也想取這個字段的值爲Validation":{"Min":"4","Max":"8"}.Like最大值爲4,最小值爲8.

任何幫助將不勝感激。

在此先感謝... :)

+0

慈祥發佈我們的代碼 – KOTIOS

+0

如果可能的話logcat – KOTIOS

+0

我希望你的真實回覆中沒有文件名中的換行符。這會打破它。 – rekire

回答

3

這裏讀閔代碼和最大值

JSONObject parentObject = new JSONObject(Json_String); 

JSONObject field1 = parentObject.getJSONObject("Field1"); 
JSONObject validation = field1.getJSONObject("Validation"); 

String min = validation.getString("Min"); 
String max = validation.getString("Max"); 

假設你的JSON字符串很好地形成。

因爲在你的迴應,你沒有得到的"\"成JSON,所以你需要一些修改成像(更新:似乎這裏並不要求更換)

String json = "{\"Field1\":{\"Field Name\":\"Phone Number\",\"Field Type\":\"Number\",\"Validation\":{\"Min\":\"4\",\"Max\":\"8\"}},\"Field2\":{\"Field Name\":\"Email\",\"Field Type\":\"String\",\"Validation\":{\"Regular Expression\":\"abcd\",\"Min Length\":\"10\",\"Max Length\":\"20\"}}}"; 

// json.replace("\\", ""); // I verified that parsing works well without replacement too 

try { 
    JSONObject parentObject = new JSONObject(json); 
    JSONObject field1 = parentObject.getJSONObject("Field1"); 
    JSONObject validation = field1.getJSONObject("Validation"); 

    String min = validation.getString("Min"); 
    String max = validation.getString("Max"); 

    System.out.println("Min ::::::: " + min); 
    System.out.println("MAx ::::::: " + max); 
    } catch (JSONException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
+0

您的線路json.replace(「\\」,「」);在這個例子中什麼都不做。 – rekire

+0

@rekire是的你是對的。我證實並更新了我的答案。非常感謝,糾正我。再次感謝 –