问题
When i send request to server. I get result data with this format:
{
"menu": {
"7": [{
"m_id": "1",
"m_flag": "1",
"m_type": "7",
"m_name": "\u30cf\u30a4\u30cd\u30b1\u30f3",
"m_price": "1000",
"m_cost": "158",
"m_regist_date": "0000-00-00",
"p_id": "0"
}, {
"m_id": "2",
"m_flag": "1",
"m_type": "7",
"m_name": "\u30ae\u30cd\u30b9",
"m_price": "1000",
"m_cost": "250",
"m_regist_date": "0000-00-00",
"p_id": "0"
},....
"2": [{
"m_id": "149",
"m_flag": "1",
"m_type": "2",
"m_name": "\u30da\u30fc\u30bf\u30fc\u30e4\u30b3\u30d6\u30ea\u30fc\u30b9\u30ea\u30f3\u30b0",
"m_price": "6500",
"m_cost": "2100",
"m_regist_date": "0000-00-00",
"p_id": "0"
}, {
"m_id": "150",
"m_flag": "1",
"m_type": "2",
"m_name": "\u30a4\u30d3\u30b9\u30af\u30b9 \u30eb\u30fc\u30b8\u30e5 08",
"m_price": "6800",
"m_cost": "2520",
"m_regist_date": "0000-00-00",
"p_id": "0"
},...
}
It very long . So when i used :
JSONObject json = jsonParser.makeHttpRequest(REQUEST_URL, "POST",params);
menu = json.getJSONArray("menu");
I get error and can't get data of JSON:
Error parsing data org.json.JSONException: End of input at character 0 of
How can i Parse data of json , I do not know how to use Gson. AND how can i get data of KEY "2". Thank you very much!
回答1:
If you are looking for GSON parsing then find it here.
Simply create a POJO class and map it to JSON string.
Sample code:
class MenuDetail {
private String m_id;
private String m_flag;
private String m_type;
private String m_name;
private String m_price;
private String m_cost;
private String m_regist_date;
private String p_id;
// getter & setter
}
...
Type type = new TypeToken<Map<String, Map<String, ArrayList<MenuDetail>>>>() {}.getType();
Map<String, Map<String, ArrayList<MenuDetail>>> data = new Gson().fromJson(json, type);
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(data));
output:
{
"menu": {
"7": [
{
"m_id": "1",
"m_flag": "1",
"m_type": "7",
"m_name": "name",
"m_price": "1000",
"m_cost": "158",
"m_regist_date": "0000-00-00",
"p_id": "0"
},
{
"m_id": "2",
"m_flag": "1",
"m_type": "7",
"m_name": "name",
"m_price": "1000",
"m_cost": "250",
"m_regist_date": "0000-00-00",
"p_id": "0"
}
]
}
}
来源:https://stackoverflow.com/questions/24133110/how-can-i-parse-json-from-webservice-result-with-gson