2012-06-11 70 views
0

我正在從網站上刪除數據。我們得到的數據是在解析Android中的JSON時出錯

[ 
[ 
    { 
     "train": "South Coast train (CityRail) ", 
     "depTime": "7:26pm", 
     "depStation": "Albion Park Station ", 
     "depPlatform": "1", 
     "arrTime": "7:46pm", 
     "arrStation": "Wollongong Station ", 
     "arrPlatform": "1" 
    }, 
    { 
     "train": "South Coast train (CityRail) ", 
     "depTime": "8:00pm", 
     "depStation": "Wollongong Station ", 
     "depPlatform": "1", 
     "arrTime": "9:14pm", 
     "arrStation": "Hurstville Station ", 
     "arrPlatform": "3" 
    }, 
    { 
     "train": "Eastern Suburbs and Illawarra train (CityRail) ", 
     "depTime": "9:20pm", 
     "depStation": "Hurstville Station ", 
     "depPlatform": "3", 
     "arrTime": "9:24pm", 
     "arrStation": "Kogarah Station ", 
     "arrPlatform": "3" 
    }, 
    { 
     "train": "Eastern Suburbs and Illawarra train (CityRail) ", 
     "depTime": "9:27pm", 
     "depStation": "Kogarah Station ", 
     "depPlatform": "4", 
     "arrTime": "9:31pm", 
     "arrStation": "Allawah Station ", 
     "arrPlatform": "4" 
    } 
], 
[ 
    { 
     "train": "South Coast train (CityRail) ", 
     "depTime": "7:26pm", 
     "depStation": "Albion Park Station ", 
     "depPlatform": "1", 
     "arrTime": "7:46pm", 
     "arrStation": "Wollongong Station ", 
     "arrPlatform": "1" 
    }, 
    { 
     "train": "South Coast train (CityRail) ", 
     "depTime": "8:00pm", 
     "depStation": "Wollongong Station ", 
     "depPlatform": "1", 
     "arrTime": "9:14pm", 
     "arrStation": "Hurstville Station ", 
     "arrPlatform": "3" 
    }, 
    { 
     "train": "Eastern Suburbs and Illawarra train (CityRail) ", 
     "depTime": "9:30pm", 
     "depStation": "Hurstville Station ", 
     "depPlatform": "3", 
     "arrTime": "9:32pm", 
     "arrStation": "Allawah Station ", 
     "arrPlatform": "3" 
    } 
], 
[ 
    { 
     "train": "South Coast train (CityRail) ", 
     "depTime": "8:16pm", 
     "depStation": "Albion Park Station ", 
     "depPlatform": "1", 
     "arrTime": "8:38pm", 
     "arrStation": "Wollongong Station ", 
     "arrPlatform": "1" 
    }, 
    { 
     "train": "South Coast train (CityRail) ", 
     "depTime": "8:54pm", 
     "depStation": "Wollongong Station ", 
     "depPlatform": "1", 
     "arrTime": "10:14pm", 
     "arrStation": "Hurstville Station ", 
     "arrPlatform": "3" 
    }, 
    { 
     "train": "Eastern Suburbs and Illawarra train (CityRail) ", 
     "depTime": "10:30pm", 
     "depStation": "Hurstville Station ", 
     "depPlatform": "3", 
     "arrTime": "10:32pm", 
     "arrStation": "Allawah Station ", 
     "arrPlatform": "3" 
    } 
], 
[ 
    { 
     "train": "South Coast train (CityRail) ", 
     "depTime": "9:23pm", 
     "depStation": "Albion Park Station ", 
     "depPlatform": "1", 
     "arrTime": "9:45pm", 
     "arrStation": "Wollongong Station ", 
     "arrPlatform": "1" 
    }, 
    { 
     "train": "South Coast train (CityRail) ", 
     "depTime": "9:54pm", 
     "depStation": "Wollongong Station ", 
     "depPlatform": "1", 
     "arrTime": "11:14pm", 
     "arrStation": "Hurstville Station ", 
     "arrPlatform": "3" 
    }, 
    { 
     "train": "Eastern Suburbs and Illawarra train (CityRail) ", 
     "depTime": "11:30pm", 
     "depStation": "Hurstville Station ", 
     "depPlatform": "3", 
     "arrTime": "11:32pm", 
     "arrStation": "Allawah Station ", 
     "arrPlatform": "3" 
    } 
], 
{ 
    "1": { 
     "train": "Eastern Suburbs and Illawarra train (CityRail) ", 
     "depTime": "5:09am+", 
     "depStation": "Hurstville Station ", 
     "depPlatform": "3", 
     "arrTime": "5:11am+", 
     "arrStation": "Allawah Station ", 
     "arrPlatform": "3" 
    } 
} 

]

的格式,當我上面的數據轉換成JSON格式我收到以下錯誤

06-11 18:36:40.584: V/Error(657): Error in parsing Innerorg.json.JSONException: Value {"1":{"depTime":"5:09am+","arrStation":"Allawah Station ","arrTime":"5:11am+","train":"Eastern Suburbs and Illawarra train (CityRail) ","arrPlatform":"3","depStation":"Hurstville Station ","depPlatform":"3"}} at 4 of type org.json.JSONObject cannot be converted to JSONArray 

如何分析這種類型的json Android中的字符串。任何建議朋友........?

+1

你上面的json字符串無效粘貼[這裏看看](http://jsonlint.com/) –

+0

你的json格式是錯誤的。你可以在http://jsonlint.com/ –

+0

上驗證它所有相同的答案: D – StErMi

回答

0

我的字符串就像下面

{ 
"result": "success", 
"countryCodeList": [ 
{ 
"countryName": "World Wide", 
"countryCode": "00" 
}, 
{ 
"countryName": "Korea, Republic of", 
"countryCode": "kr" 
}, 
{ 
"countryName": "United States", 
"countryCode": "us" 
},- 
{ 
"countryName": "Japan", 
"countryCode": "jp" 
}, 
{ 
"countryName": "China", 
"countryCode": "cn" 
}, 
{ 
"countryName": "India", 
"countryCode": "in" 
} 
] 
} 

我做

JSONObject json = new JSONObject(jsonstring); 
JSONArray nameArray = json.names(); 
JSONArray valArray = json.toJSONArray(nameArray); 

JSONArray valArray1 = valArray.getJSONArray(1); 

    valArray1.toString().replace("[", ""); 
      valArray1.toString().replace("]", ""); 

      int len = valArray1.length(); 

      for (int i = 0; i < valArray1.length(); i++) { 

       Country country = new Country(); 
       JSONObject arr = valArray1.getJSONObject(i); 

       country.setCountryCode(arr.getString("countryCode")); 
       country.setCountryName(arr.getString("countryName")); 
       arrCountries.add(country); 
      } 
+0

我剛進入我的字符串的一部分。實際上它是一個有效的json格式。我已經在http://www.jsonlint.org/中檢查過。如果你想,你可以現在檢查我的字符串 –

1

這是因爲你的JSON字符串構造不好。

試着把它放到這裏:http://jsonlint.com/它會告訴你目前的錯誤。

也請閱讀JSON在這裏http://json.org/一些單證因爲你實際上是做一些基本的錯誤:)

如果您還提供你的JSON有rappresent我可以告訴你的JSON字符串:)

的正確形式
+1

對。這個Json包含2個根元素,這是無效的。 – Jokahero