我怎样才能从Gson的Webservice结果解析JSON [英] How can i parse JSON from Webservice result with Gson
问题描述
当我发送请求到服务器。
我以这种格式得到结果数据:
{
menu:{
7:[{
m_id:1,
m_flag:1,
m_type:7,
m_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:\\\ギ\\\ネ\\\ス,
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\\\ー\\\タ\\\ー\\\ヤ\\\コ\\\ブ\\\リ\\\ー\\\ス\\\リ\\\ン\\\グ,
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:\\\イ\\\ビ\\ \\ u30b9 \\\ク \\\ス \\\ル\\\ー\\\ジ\\\ュ 08,
m_price:6800,
m_cost:2520,
m_regist_date:0000-00-00,
p_id:0
},...
}
它很长。所以当我使用:
JSONObject json = jsonParser.makeHttpRequest(REQUEST_URL,POST,params);
menu = json.getJSONArray(menu);
我得到错误,无法获取JSON的数据:
解析数据时出错org.json.JSONException:
我如何解析json的数据,我不知道如何使用Gson。
以及如何获取KEY2的数据。
非常感谢!
如果您正在寻找 只需创建一个POJO类并将其映射到JSON字符串即可。 示例代码: 输出: GSON $ c
class MenuDetail {
private String m_id;
private String m_flag;
private String m_type;
private String m_name;
私人字符串m_price;
private String m_cost;
私人字符串m_regist_date;
私人字符串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));
< code $ {
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
}
]
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!
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"
}
]
}
}
这篇关于我怎样才能从Gson的Webservice结果解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!