首先要说明上面所说的 JSON 代码格式不正确。您可以通过转到http://json.parser.online.fr/ 来验证您的 JSON 代码格式是否正确。
您正确的 JSON 如下(您缺少结束 })
{
"ics_desire": {
"version": "Beta 0.1.1",
"description": "description here",
"build-fingerprint": "fingerprint"
}
}
接下来,这是我过去用来测试的有效 JSON 代码示例。
{
"HealthySubstituteData": [
{
"Assoc_ID": "1",
"uFood": "White Flour",
"hFood": "Wheat Flour",
"Category": "Baking",
"Description": "You can substitute blah blah blah...",
"Count": "5",
"submittedBy": "Administrator"
}
]
}
这是我用来获取数据的代码。
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL("http://your.url.com/whaterver");
try
{
JSONArray healthySubstituteData = json.getJSONArray("HealthySubstituteData");
for(int i=0;i<healthySubstituteData.length();i++)
{
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = healthySubstituteData.getJSONObject(i);
map.put("Assoc_ID", (String) e.get("Assoc_ID"));
map.put("uFood", (String) e.get("uFood"));
map.put("hFood", (String) e.get("hFood"));
map.put("category", (String) e.get("Category"));
map.put("description", (String) e.get("Description"));
map.put("count", (String) e.get("Count"));
map.put("submittedBy", (String) e.get("submittedBy"));
mylist.add(map);
}
}
所以现在我得到了一个 HashMap 类型的数组列表,那时我可以用它做任何我想做的事情。