我正在尝试从json文件中读取,该文件在JSONObject内部有JSONArray,它正在读取第一个JSONObject值而不是从内部读取的内容称为“zoom”,你能帮我吗?
{
"@file_name": "materials",
"materials": [
{
"@site_name": "N/A",
"@site_icon_iPad_xPosition": "150",
"@site_icon_iPad_yPosition": "150",
"@site_icon_Android_xPosition": "215",
"@site_icon_Android_yPosition": "206",
"zoom":
{
"@zoom_name": "Main Reservoir",
"@zoom_number": "zoom1"
}
},
{
"@site_name": "Building Applications",
"@site_icon_iPad_xPosition": "636",
"@site_icon_iPad_yPosition": "313",
"@site_icon_Android_xPosition": "215",
"@site_icon_Android_yPosition": "206",
"zoom": [
{
"@zoom_name": "Rooftop Reservoir & Pump Station",
"@zoom_number": "zoom10-1"
},
{
"@zoom_name": "Compact Zone PRV System",
"@zoom_number": "zoom10-2"
},
{
"@zoom_name": "Basement Reservoir & Pump Room",
"@zoom_number": "zoom10-3"
}
]
}
]
}
我的代码:
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(Environment.getExternalStorageDirectory().getPath()+"/Materials.json");
try {
JSONArray materials = json.getJSONArray("materials");
for(int i = 0; i < materials.length(); i++){
JSONObject c = materials.getJSONObject(i);
// Storing each json item in variable
String a1= c.getString("@site_name");
String a2 = c.getString("@site_icon_Android_xPosition");
String a3 = c.getString("@site_icon_Android_yPosition");
Log.d(TAG, a1+" "+a2+" "+a3);
JSONArray zoom = c.getJSONArray("zoom");
Log.d(TAG, ""+zoom.length());
for(int j = 0; j < zoom.length(); j++){
JSONObject d = zoom.getJSONObject(j);
String b1 = d.getString("@zoom_name");
String b2 = d.getString("@zoom_number");
Log.d(TAG, b1+" "+b2);
}
Log.d(TAG, "--------------------------------");
}
} catch (JSONException e) {
Log.d(TAG, "exeption");
e.printStackTrace();
}
我的输出:
Applications 215 206
exeption
例外:
03-24 11:29:01.188: W/System.err(17952): org.json.JSONException: Value {"@zoom_number":"zoom1","@zoom_name":"Main Reservoir"} at zoom of type org.json.JSONObject cannot be converted to JSONArray
03-24 11:29:01.198: W/System.err(17952): at org.json.JSON.typeMismatch(JSON.java:100)
03-24 11:29:01.198: W/System.err(17952): at org.json.JSONObject.getJSONArray(JSONObject.java:548)
03-24 11:29:01.198: W/System.err(17952): at com.weterworks.MainActivity.onCreate(MainActivity.java:120)
03-24 11:29:01.198: W/System.err(17952): at android.app.Activity.performCreate(Activity.java:4465)
03-24 11:29:01.198: W/System.err(17952): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-24 11:29:01.198: W/System.err(17952): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-24 11:29:01.198: W/System.err(17952): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
03-24 11:29:01.198: W/System.err(17952): at android.app.ActivityThread.access$600(ActivityThread.java:123)
03-24 11:29:01.198: W/System.err(17952): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
03-24 11:29:01.198: W/System.err(17952): at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 11:29:01.198: W/System.err(17952): at android.os.Looper.loop(Looper.java:137)
03-24 11:29:01.198: W/System.err(17952): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-24 11:29:01.198: W/System.err(17952): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 11:29:01.198: W/System.err(17952): at java.lang.reflect.Method.invoke(Method.java:511)
03-24 11:29:01.198: W/System.err(17952): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-24 11:29:01.198: W/System.err(17952): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-24 11:29:01.198: W/System.err(17952): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
您的代码中有一个小错误。在处理zoom
属性的内部循环中,您指的是错误的数组对象。
for (int j = 0; j < zoom.length(); j++) {
JSONObject d = materials.getJSONObject(j); // <-- Should refer to zoom, not materials
String b1 = d.getString("@zoom_name");
String b2 = d.getString("@zoom_number");
Log.d(TAG, b1 + " " + b2);
}