有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓解析JSON。java,如果没有数组名,解析结果是什么?

这是我的代码:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Belal on 9/22/2015.
 */
public class ParseJSON {
    public static String[] make;
    public static String[] model;
    public static String[] sixMonthRate;

    public static final String JSON_ARRAY = "result";
    public static final String KEY_MAKE = "make";
    public static final String KEY_MODEL = "model";
    public static final String KEY_SIXMONTHRATE = "sixMonthRate";

    private JSONArray vehicle = null;

    private String json;

    public ParseJSON(String json){
        this.json = json;
    }

    protected void parseJSON(){
        JSONObject jsonObject=null;
        try {
            jsonObject = new JSONObject(json);
            vehicle = jsonObject.getJSONArray(JSON_ARRAY);

            make = new String[vehicle.length()];
            model = new String[vehicle.length()];
            sixMonthRate = new String[vehicle.length()];

            for(int i=0;i<vehicle.length();i++){
                JSONObject jo = vehicle.getJSONObject(i);
                make[i] = jo.getString(KEY_MAKE);
                model[i] = jo.getString(KEY_MODEL);
                sixMonthRate[i] = jo.getString(KEY_SIXMONTHRATE);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

如果我将使用DVLA JSON DATABASE而没有数组名,我应该如何更改代码以使其工作

目前,如果我按下按钮获取数据,我的应用程序已关闭

我正在使用这个教程-HERE

错误获取

03-26 02:49:43.467 23673-23673/com.sandro.openalprsample W/System.err: org.json.JSONException: No value for result
03-26 02:49:43.467 23673-23673/com.sandro.openalprsample W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
03-26 02:49:43.467 23673-23673/com.sandro.openalprsample W/System.err:     at org.json.JSONObject.getJSONArray(JSONObject.java:584)
03-26 02:49:43.467 23673-23673/com.sandro.openalprsample W/System.err:     at com.sandro.openalprsample.ParseJSON.parseJSON(ParseJSON.java:42)
03-26 02:49:43.467 23673-23673/com.sandro.openalprsample W/System.err:     at com.sandro.openalprsample.DVLAresult.showJSON(DVLAresult.java:60)
03-26 02:49:43.467 23673-23673/com.sandro.openalprsample W/System.err:     at com.sandro.openalprsample.DVLAresult.access$000(DVLAresult.java:18)
03-26 02:49:43.472 23673-23673/com.sandro.openalprsample W/System.err:     at com.sandro.openalprsample.DVLAresult$1.onResponse(DVLAresult.java:44)
03-26 02:49:43.472 23673-23673/com.sandro.openalprsample W/System.err:     at com.sandro.openalprsample.DVLAresult$1.onResponse(DVLAresult.java:41)
03-26 02:49:43.472 23673-23673/com.sandro.openalprsample W/System.err:     at com.安卓.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
03-26 02:49:43.472 23673-23673/com.sandro.openalprsample W/System.err:     at com.安卓.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
03-26 02:49:43.472 23673-23673/com.sandro.openalprsample W/System.err:     at com.安卓.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
03-26 02:49:43.472 23673-23673/com.sandro.openalprsample W/System.err:     at 安卓.os.Handler.handleCallback(Handler.java:739)
03-26 02:49:43.472 23673-23673/com.sandro.openalprsample W/System.err:     at 安卓.os.Handler.dispatchMessage(Handler.java:95)
03-26 02:49:43.472 23673-23673/com.sandro.openalprsample W/System.err:     at 安卓.os.Looper.loop(Looper.java:145)
03-26 02:49:43.472 23673-23673/com.sandro.openalprsample W/System.err:     at 安卓.app.ActivityThread.main(ActivityThread.java:5832)
03-26 02:49:43.472 23673-23673/com.sandro.openalprsample W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
03-26 02:49:43.472 23673-23673/com.sandro.openalprsample W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
03-26 02:49:43.472 23673-23673/com.sandro.openalprsample W/System.err:     at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
03-26 02:49:43.472 23673-23673/com.sandro.openalprsample W/System.err:     at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
03-26 02:49:43.472 23673-23673/com.sandro.openalprsample D/AndroidRuntime: Shutting down VM
03-26 02:49:43.472 23673-23673/com.sandro.openalprsample E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.sandro.openalprsample, PID: 23673
                                                                           java.lang.NullPointerException: storage == null
                                                                               at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
                                                                               at java.util.Arrays.asList(Arrays.java:155)
                                                                               at 安卓.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
                                                                               at com.sandro.openalprsample.CustomList.<init>(CustomList.java:21)
                                                                               at com.sandro.openalprsample.DVLAresult.showJSON(DVLAresult.java:61)
                                                                               at com.sandro.openalprsample.DVLAresult.access$000(DVLAresult.java:18)
                                                                               at com.sandro.openalprsample.DVLAresult$1.onResponse(DVLAresult.java:44)
                                                                               at com.sandro.openalprsample.DVLAresult$1.onResponse(DVLAresult.java:41)
                                                                               at com.安卓.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
                                                                               at com.安卓.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
                                                                               at com.安卓.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
                                                                               at 安卓.os.Handler.handleCallback(Handler.java:739)
                                                                               at 安卓.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at 安卓.os.Looper.loop(Looper.java:145)
                                                                               at 安卓.app.ActivityThread.main(ActivityThread.java:5832)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                                               at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                                                                               at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

共 (1) 个答案

  1. # 1 楼答案

    DVLA JSON数据库应该是JSON_数组的内容吗

    如果是这样,那么你应该调用getJsonObject而不是getJsonArray,然后像这样迭代它们:

    for(String key : vehicle.keySet() {
        // Do something with the key and object.
        Object obj = vehicle.get(key);
    }
    

    如果不是,我想我没有完全理解你的问题

    已编辑

    我不确定从哪里得到你的“json”值,但我假设它是DVLA_json_数据库的内容

    如果没有,只需复制粘贴以下内容:

    JSONObject jsonObject = new JSONObject("{\"make\":\"VOLKSWAGEN\",\"model\":\"Tiguan\",\"sixMonthRate\":\"Unknown\",\"twelveMonthRate\":\"Unknown\",\"dateOfFirstRegistration\":\"23 July 2009\",\"yearOfManufacture\":\"2009\",\"cylinderCapacity\":\"1968cc\",\"co2Emissions\":\"167 g/km\",\"fuelType\":\"DIESEL\",\"taxStatus\":\"Taxed and due\",\"colour\":\"SILVER\",\"typeApproval\":\"M1\",\"wheelPlan\":\"2 AXLE RIGID BODY\",\"revenueWeight\":\"Not available\",\"taxDetails\":\"Tax due: 01 April 2016\",\"motDetails\":\"Expires: 21 April 2016\",\"taxed\":true,\"mot\":true}");
    

    代替json

    下面是一些代码:

    try {
            jsonObject = new JSONObject(json);
    
            String make = jsonObject.getString("make");
            String model = jsonObject.getString("model");
            String sixMonthRate = jo.getString("sixMonthRate");
            // Do something with these variables
    
        } catch (JSONException e) {
            e.printStackTrace();
        }
    

    你可能已经注意到了,但是JSON由key:value组成。 只需调用getString(key)即可获得字符串值。 类似地,您可以调用“getInt(key)”来获取整数值

    就这些