如何将JSON对象转换为JSON数组?

时间:2017-09-14 20:45:13

标签: android arrays json

如何将此转换为JSONObject到JSONArray以在Android的ListView中列出它?

{
  "42": "42 Coin",
  "365": "365Coin",
  "404": "404Coin",
  "611": "SixEleven",
  "808": "808",
  "888": "Octocoin",
  "1337": "1337",
  "2015": "2015 coin",
  "CLUB": " ClubCoin",
  "007": "007 coin",
  "ZRX": "0x",
  "BIT16": "16BitCoin"
}

我尝试使用以下代码

 try {

        String resp = loadJSONFromAsset();
        JSONObject ob = new JSONObject(resp);
        JSONArray arr = ob.getJSONArray("");

        for(int i=0; i<arr.length(); i++){
            JSONObject o = arr.getJSONObject(i);
            System.out.println(o);
        }


    }
catch (JSONException e) {
    e.printStackTrace();
}

但显示以下错误

09-15 02:13:10.947 2296-2296/com.example.alexmathew.myapplication W/System.err: org.json.JSONException: No value for 
09-15 02:13:10.947 2296-2296/com.example.alexmathew.myapplication W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
09-15 02:13:10.947 2296-2296/com.example.alexmathew.myapplication W/System.err:     at org.json.JSONObject.getJSONArray(JSONObject.java:584)
09-15 02:13:10.947 2296-2296/com.example.alexmathew.myapplication W/System.err:     at com.example.alexmathew.myapplication.MainActivity.onCreate(MainActivity.java:47)
09-15 02:13:10.947 2296-2296/com.example.alexmathew.myapplication W/System.err:     at android.app.Activity.performCreate(Activity.java:6662)
09-15 02:13:10.947 2296-2296/com.example.alexmathew.myapplication W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
09-15 02:13:10.947 2296-2296/com.example.alexmathew.myapplication W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
09-15 02:13:10.947 2296-2296/com.example.alexmathew.myapplication W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
09-15 02:13:10.947 2296-2296/com.example.alexmathew.myapplication W/System.err:     at android.app.ActivityThread.-wrap12(ActivityThread.java)
09-15 02:13:10.947 2296-2296/com.example.alexmathew.myapplication W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
09-15 02:13:10.947 2296-2296/com.example.alexmathew.myapplication W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
09-15 02:13:10.947 2296-2296/com.example.alexmathew.myapplication W/System.err:     at android.os.Looper.loop(Looper.java:154)
09-15 02:13:10.947 2296-2296/com.example.alexmathew.myapplication W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6077)
09-15 02:13:10.947 2296-2296/com.example.alexmathew.myapplication W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-15 02:13:10.948 2296-2296/com.example.alexmathew.myapplication W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
09-15 02:13:10.948 2296-2296/com.example.alexmathew.myapplication W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
09-15 02:13:11.884 2296-2296/com.example.alexmathew.myapplication W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
09-15 02:13:11.978 2296-2334/com.example.alexmathew.myapplication D/EGL_emulation: eglMakeCurrent: 0xabf053c0: ver 2 0 (tinfo 0xabf03570)

如何使此代码有效?我知道哪里出错了?

1 个答案:

答案 0 :(得分:1)

{   "coin": [
    {
      "42": "42 Coin"
    },
    {
      "365": "365Coin"
    },
    {
      "404": "404Coin"
    },
    {
      "611": "SixEleven"
    },
    {
      "808": "808"
    },
    {
      "888": "Octocoin"
    },
    {
      "1337": "1337"
    },
    {
      "2015": "2015 coin"
    },
    {
      "CLUB": " ClubCoin"
    },
    {
      "007": "007 coin"
    },
    {
      "ZRX": "0x"
    },
    {
      "BIT16": "16BitCoin"
    }   ] }

尝试使用以下代码

try{
 String resp = loadJSONFromAsset();
            JSONObject ob = new JSONObject(resp);
            JSONArray arr = ob.getJSONArray("coin");

            for(int i=0; i<arr.length(); i++){
                JSONObject o = arr.getJSONObject(i);
                System.out.println(o);
            }


        }catch (JSONException e) {
            e.printStackTrace();
        }