Android 如何解析以下JSON?
嗨,我是JSON格式Web服务的新手。 我有一个类似于Android 如何解析以下JSON?,android,json,Android,Json,嗨,我是JSON格式Web服务的新手。 我有一个类似于 { "meta":{"success":1}, "Countries": [[{"Id":"1","Name":"Gibraltar", "Cities": [[{"Id":"21","Name":"Gibraltar"}]] }, {"Id":"2","Name":"Canada", "Cities": [[{"Id":"22","Name":"Toronto"}, {"I
{ "meta":{"success":1},
"Countries":
[[{"Id":"1","Name":"Gibraltar",
"Cities":
[[{"Id":"21","Name":"Gibraltar"}]]
},
{"Id":"2","Name":"Canada",
"Cities":
[[{"Id":"22","Name":"Toronto"},
{"Id":"39","Name":"Banff"}]]
},
{"Id":"4","Name":"Malta",
"Cities":
[[{"Id":"37","Name":"Valletta"}]]
},
{"Id":"51","Name":"Italy",
"Cities":
[[{"Id":"24","Name":"Sardinia"}]]
},
{"Id":"53","Name":"England",
"Cities":
[[{"Id":"23","Name":"London"},
{"Id":"38","Name":"Guildford"},
{"Id":"43","Name":"Petersfield"},
{"Id":"44","Name":"Isle of Wight"}]]
},
{"Id":"175","Name":"Hungary",
"Cities":
[[{"Id":"36","Name":"Budapest"}]]
}
]]
}
但是我无法得到这些值。。当解析为json对象时
我试着得到的价值观像
public class JSONParsing1 extends ListActivity {
private static String url = "http://wsapi.vr2020.com/countries.json";
private static final String TAG_META = "meta";
private static final String TAG_SUCCESS = "success";
private static final String TAG_COUNTRIES = "Countries";
private static final String TAG_ID = "Id";
private static final String TAG_NAME = "Name";
private static final String TAG_CITY = "Cities";
private static final String TAG_CITY_ID = "Id";
private static final String TAG_CITY_NANE = "Name";
private String id;
private String name;
private String city_id;
private String city_name;
JSONObject meta;
JSONArray Countries = null;
JSONArray Cities = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = jsonParser.getJSONFromUrl(url);
try {
meta = jsonObject.getJSONObject(TAG_META);
TextView startText = (TextView) findViewById(R.id.stat_textView);
startText.setText(meta.getString(TAG_SUCCESS));
} catch (Exception e) {
e.printStackTrace();
}
try {
Countries = meta.getJSONArray(TAG_COUNTRIES);
for (int i = 0; i < Countries.length(); i++) {
JSONObject obj = Countries.getJSONObject(i);
id = obj.getString(TAG_ID);
name = obj.getString(TAG_NAME);
JSONArray city = obj.getJSONArray(TAG_CITY);
for(int j = 0; j< city.length(); j++){
JSONObject cityobj = city.getJSONObject(j);
city_id = cityobj.getString(TAG_CITY_ID);
city_name = cityobj.getString(TAG_CITY_ID);
}
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
list.add(map);
}
} catch (Exception e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(this, list,
R.layout.list_items, new String[] { TAG_ID, TAG_NAME,
}, new int[] { R.id.id_textView,
R.id.name_textView, R.id.description_textView });
setListAdapter(adapter);
}
}
任何人都可以分享如何解析上述JSON吗?
提前感谢。将当前Json解析为:
JSONObject jsonObj = new JSONObject(jsonStr);
// these 2 are strings
JSONObject c = jsonObj.getJSONObject("meta");
String success = c.getString("success");
JSONArray jsonarr = jsonObj.getJSONArray("Countries");
// lets loop through the JSONArray and get all the items
for (int i = 0; i < jsonarr.length(); i++) {
JSONArray jsonarra = jsonarr.getJSONArray(i);
// lets loop through the JSONArray and get all the items
for (int j = 0; j < jsonarra.length(); j++) {
JSONObject jsonarrtwo = jsonarra.getJSONObject(j);
// these 2 are strings
String str_id = jsonarrtwo.getString("id");
String str_Name = jsonarrtwo.getString("Name");
JSONArray jsonarr_cities = jsonarrtwo.getJSONArray("Cities");
// lets loop through the JSONArray and get all the items
for (int t = 0; t < jsonarr_cities.length(); t++) {
// printing the values to the logcat
JSONArray jsonarr_cities_one = jsonarrtwo.getJSONArray(t);
for (int tt = 0; tt < jsonarr_cities_one.length(); tt++) {
// printing the values to the logcat
JSONObject jsonarr_cities_two = jsonarr_cities_one.getJSONObject(tt);
// these 2 are strings
String str_idone = jsonarr_cities_two.getString("id");
String str_Nameone = jsonarr_cities_two.getString("Name");
}
}
}
}
JSONObject jsonObj=新的JSONObject(jsonStr);
//这两个是字符串
JSONObject c=jsonObj.getJSONObject(“meta”);
String success=c.getString(“success”);
JSONArray jsonarr=jsonObj.getJSONArray(“国家”);
//让我们在JSONArray中循环并获取所有项
对于(inti=0;i
要解析json,请尝试以下步骤:
要格式化json,请执行以下操作:
您可以使用此工具验证json字符串 您的json字符串包含一个json数组,json数组位于国家标记和城市标记处。 在您的
JSONParsing1
活动中尝试类似的操作
替换代码行
Countries = meta.getJSONArray(TAG_COUNTRIES);
JSONArray city = obj.getJSONArray(TAG_CITY);
用这条线
Countries = new JSONArray(jsonObject.getJSONArray(TAG_COUNTRIES).toString());
JSONArray city = new JSONArray(obj.getJSONArray(TAG_CITY).toString());
替换代码行
Countries = meta.getJSONArray(TAG_COUNTRIES);
JSONArray city = obj.getJSONArray(TAG_CITY);
用这条线
Countries = new JSONArray(jsonObject.getJSONArray(TAG_COUNTRIES).toString());
JSONArray city = new JSONArray(obj.getJSONArray(TAG_CITY).toString());
它可能会帮助你解决这个问题。这样试试……;)
但是我无法获得值,你尝试了什么?JSON似乎很好,你有什么问题吗?点击这个链接..Ganesh:你能展示一下你当前使用的解析器函数代码吗?我已经和解析器类一起编辑了我的帖子谢谢你,JSONArray jsonarr\u cities\u one=jsonarrtwo.getJSONArray(t);在上面的getJSONArray(t)方法中,t作为整数不被接受。@Ganesh:tryjsonArray.get(t)
而不是jsonarrtwo.getJSONArray(t)代码>您可以做一些更改,但按照您提供的方式解析json字符串是正确的。谢谢