无法解析JSON

阿舒托什·辛格(Ashutosh singh)

我是android的初学者,如何传递此JSON http://coinabul.com/api.php并在主要活动的文本框中显示所有分类的USD项目,当im显示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);
}
深层

请在下面找到方法。您只需要在内部传递响应字符串即可。如果您发现任何问题,请告诉我:

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
             }
         }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章