我是android的初学者如何传递这个JSON http://coinabul.com/api.php并在主要活动的文本框中显示所有catageory的USD项目,当我显示log usd值时显示210三个键但是在json中它有三个不同的值
protected void onPostExecute(InputStream result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progress.dismiss();
String response = ConvertStreamIntoString.convertStreamToString(result);
Dictionary<String, Dictionary<String, String>> topDictionary = new Hashtable<String, Dictionary<String, String>>();
Dictionary<String, String> innerDictionary = new Hashtable<String, String>();
try {
JSONObject jsobject = new JSONObject(response);
Iterator<String> iterator = jsobject.keys();
while (iterator.hasNext()) {
String key = iterator.next();
// String value = getString(jsobject, key);
String v2=null;
JSONObject jsonObject = jsobject.getJSONObject(key);
Iterator<String> iterator123 = jsonObject.keys();
while (iterator123.hasNext()) {
String keysss = (String) iterator123.next();
String value = getString(jsonObject, keysss);
innerDictionary.put(keysss, value);
Log.e("innerDict",innerDictionary.toString());
}
topDictionary.put(key, innerDictionary);
Log.e("asd", topDictionary.toString());
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
context.showdata(topDictionary, innerDictionary);
}
答案 0 :(得分:0)
请找到以下方法。你只需要在里面传递响应字符串。如果您发现任何问题,请告诉我:
private void parseResponse(String response) throws JSONException {
Dictionary<String, Dictionary<String, String>> topDictionary = new Hashtable<String, Dictionary<String, String>>();
JSONObject jsobject = new JSONObject(response);
Iterator<String> iterator = jsobject.keys();
while (iterator.hasNext()) {
String key = iterator.next();
JSONObject jsonObject = jsobject.getJSONObject(key);
Iterator<String> iterator123 = jsonObject.keys();
Dictionary<String, String> innerDictionary = new Hashtable<String, String>();
while (iterator123.hasNext()) {
String keysss = iterator123.next();
String value = jsonObject.getString(keysss);
innerDictionary.put(keysss, value);
}
topDictionary.put(key, innerDictionary);
}
Log.v("topDictionary",topDictionary.toString());
showdata(topDictionary);
}
private void showdata(
Dictionary<String, Dictionary<String, String>> topDictionary) {
for (Enumeration e = topDictionary.keys(); e.hasMoreElements();) {
String keyTop = e.nextElement().toString();
Log.v("keyTop",""+keyTop);
Dictionary<String, String> innerElements = topDictionary.get(keyTop);
for (Enumeration ei = innerElements.keys(); ei.hasMoreElements();) {
String keyInner = ei.nextElement().toString();
String value = innerElements.get(keyInner);
System.out.println("key: "+keyInner+" value:"+value);
// Here you can show your values in textview
}
}
}