【问题标题】:Android JSON parsing error cannot be converted to JSONArrayAndroid JSON解析错误无法转换为JSONArray
【发布时间】:2017-03-31 05:46:12
【问题描述】:

我是 Android 开发的新手,我正在尝试编写一个程序来解析来自网站的一些 JSON 并将其输出到 ListView 中。但是,当我运行我的程序时,我得到了错误:(有超过 7 行与此错误相同)

03-31 05:25:14.296 3196-3196/nazilli.tenispark E/FAILED: Json 解析 错误:值 {"0":"2","id":"2","1":"0","cid":"0","2":"1","uid":"1"," 3":"2017-04-01","日期":"2017-04-01","4":"20","小时":"20"} org.json.JSONObject 类型的无法转换为 JSONArray

03-31 05:25:14.297 3196-3196/nazilli.tenispark E/FAILED:Json 解析错误: 价值 {"0":"3","id":"3","1":"0","cid":"0","2":"1","uid":"1"," 3":"2017-04-08","日期":"2017-04-08","4":"20","小时":"20"} org.json.JSONObject 类型的无法转换为 JSONArray

我要解析的 JSON 是:

{"约会":[{"0":"2","id":"2","1":"0","cid":"0","2":"1", "uid":"1","3":"2017-04-01","日期":"2017-04-01","4":"20","小时":"20"},{ "0":"3","id":"3","1":"0","cid":"0","2":"1","uid":"1","3 ":"2017-04-08","date":"2017-04-08","4":"20","hour":"20"},{"0":"4","id ":"4","1":"0","cid":"0","2":"1","uid":"1","3":"2017-04-15", "日期":"2017-04-15","4":"20","小时":"20"},{"0":"5","id":"5","1": "0","cid":"0","2":"1","uid":"1","3":"2017-04-22","date":"2017-04-22 ","4":"20","小时":"20"},{"0":"6","id":"6","1":"0","cid":"0 ","2":"1","uid":"1","3":"2017-03-24","date":"2017-03-24","4":"17", "小时":"17"},{"0":"7","id":"7","1":"0","cid":"0","2":"1", "uid":"1","3":"2017-03-26","日期":"2017-03-26","4":"17","小时":"17"},{ "0":"8","id":"8","1":"1","cid":"1","2":"1","uid":"1","3 ":"2017-03-26","date":"2017-03-26","4":"16","hour":"16"},{"0":"9","id ":"9","1":"2","cid":"2","2":"1","uid":"1","3":"2017-03-26", "日期":"2017-03-26","4":"15","小时":"15"},{"0":"10","id":"10","1": "3","cid":"3","2":"1","uid":"1","3":"2017-03-26","da te":"2017-03-26","4":"13","hour":"13"}]}

这是我的 listview 自定义行 java

public class adapter_appointment extends ArrayAdapter<String> {
    public adapter_appointment(Context context, String[] data){
        super(context, R.layout.row_layout, data);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View customView = inflater.inflate(R.layout.row_layout, parent, false);
        String all_data = getItem(position);
        TextView title = (TextView) customView.findViewById(R.id.title);
        //title.setText(all_data.toString());
        try {
            JSONArray array = new JSONArray(all_data);
            JSONObject obj = array.getJSONObject(0);
            Log.d("SUCCESS", "JSON Object: " + obj.toString());
            if (obj.has("date") && !obj.isNull("date")) {
                title.setText(obj.getString("date").toString());
                Log.d("SUCCESS", "Date: " + obj.getString("date").toString());
            } else {
                // Do something
            }
        } catch (Exception e) {
            Log.e("FAILED", "Json parsing error: " + e.getMessage());
        }
        return customView;
    }
}

这是 json.java

public class my_appointments extends AppCompatActivity {
    ListView lv;
    InputStream is = null;
    String line = null;
    String result = null;
    String[] data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_appointments);

        lv=(ListView) findViewById(R.id.my_appointments);
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
        //Run
        getData();
        ArrayAdapter adapter = new adapter_appointment(this, data);
        lv.setAdapter(adapter);
    }

    private void getData()
    {
        try {
            URL url = new URL("MY URL");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            is=new BufferedInputStream(con.getInputStream());
            //
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            while((line=br.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
            //
            JSONObject jo = new JSONObject(result);
            JSONArray ja = jo.getJSONArray("appointments");
            data = new String[ja.length()];
            for(int i=0;i<ja.length();i++)
            {
                jo=ja.getJSONObject(i);
                data[i]=jo.toString();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

如果我不解析:

【问题讨论】:

  • 因为all_dataJSONObject 而不是JSONArray。使用 JSONObject jsonObject = new JSONObject(all_data); 然后使用 jsonObject 从中获取所有 JSONArrays
  • 感谢您的快速回复,我该如何解决这个问题。
  • 检查我的答案。如果有帮助,请点击接受

标签: android json parsing


【解决方案1】:

你的appointmentsJSONArray

JSON

{"约会":[{"0":"2","id":"2","1":"0","cid":"0","2":"1", "uid":"1","3":"2017-04-01","日期":"2017-04-01","4":"20","小时":"20"}]}

 JSONObject reader = new JSONObject(all_data);
 JSONArray jsonArray = reader.getJSONArray("appointments");
  ......//Do your work//..........

【讨论】:

  • 它给出了一个错误 E/FAILED: Json parsing error: No value for dating
  • 不,我正在尝试一一回答。
【解决方案2】:
JSONObject jsonObject= new JSONObject(all_data);

JSONArray jsonArray = jsonObject.getJSONArray("appointments");
 // To get elements from the array 
for(int i=0;i<jsonArray.length;i++){
  JSONObject json = jsonArray.getJSONObject(i);
  String id = json.getString("id");
  String name=json.getString("cid");
}

【讨论】:

  • 我仍然收到错误 E/FAILED:Json 解析错误:约会没有价值
【解决方案3】:
JSONObject json = new JSONObject(all_data);
 JSONArray jsonArray = json.getJSONArray("appointments");

您应该从 all_data 中获取数组,并使用该数组而不是 json

【讨论】:

    【解决方案4】:

    您可以创建一个具有 JSON 结构的类,并使用 GSON 来获取类似 User user = gson.fromJson(json, User.class) 的数据

    【讨论】:

      【解决方案5】:

      您的所有数据都有以下数据。如下所示。

       alldata={"0":"2","id":"2","1":"0","cid":"0","2":"1","uid":"1","3":"2017-04-01","date":"2017-04-01","4":"20","hour":"20"}
      

      因此,每次您的列表项都从该约会数组中获取数据。因此 alldata 变量将根据 getItem(position) 方法中的位置进行更改。

      所以,所有数据都是 jsonObject。

      所以你应该像下面这样解析,

       JSONObject jsonRowData= new JSONObject(allData);
       try{
         jsonRowData.getString("0");
         jsonRowData.getString("id"):
         jsonRowData.getString("1"):
         jsonRowData.getString("cid"):
         jsonRowData.getString("2"):
         jsonRowData.getString("uid"):
         jsonRowData.getString("3");
         jsonRowData.getString("date"):
         jsonRowData.getString("4");
          }catch(Exception e){
          e.printStackTrace();
         }
      

      就像你必须打电话一样。希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-25
        • 2018-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多