-1
如何將此轉換爲JSONObject到JSONArray以將其列在Android的ListView中?如何將JSON對象轉換爲JSON數組?
{
"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)
如何才能使此代碼工作?任何想法,我錯了?
這不是一個數組。這是一個JSON對象 – joao86
@ joao86:任何想法如何我可以使用上面的JSON列表在ListView中? –
試試這個:https://stackoverflow.com/questions/13573913/android-jsonobject-how-can-i-loop-through-a-flat-json-object-to-get-each-key-a – joao86