java.lang.String无法转换为JSONObject。不匹配类型

时间:2017-01-19 06:35:59

标签: android json parsing

JSON Array

 [
  {
    "0": {
      "program_name": "Daycare"
    },
    "1": {
      "program_name": "Preschool"
    },
    "program_name": [
      {
        "program_name": "Daycare"
      },
      {
        "program_name": "Preschool"
      }
    ],
    "batch_name": [
      {
        "0": "3 Hours",
        "batch_class_name": "3 Hours"
      },
      {
        "0": "5 Hours",
        "batch_class_name": "5 Hours"
      }
    ]
  }
]

这是我到目前为止所做的: -

void getProgram() {

    progressDialog = new MaterialDialog.Builder(getActivity())
            .content("Please wait....")
            .progress(true, 0)
            .show();
    StringRequest stringRequest = new StringRequest(Request.Method.POST, GlobalConfig.GET_PROGRAM_AND_BATCH_OF_TEACHER,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    progressDialog.dismiss();
                    Log.e("response", response);
                    try {



                        JSONArray jsonArray = new JSONArray(response);
                        JSONObject jsonObject = jsonArray.getJSONObject(0);
                        JSONArray jsonProgramArray = jsonObject.getJSONArray("program_name");
                        for (int i = 0; i < jsonObject.length() - 1; i++) {

                            BatchModel programe = new BatchModel();
                            programe.setTitle(jsonProgramArray.getString(i));
                            programe.setId(jsonProgramArray.getString(i));
                            programlist.add(programe);
                            Log.e("Program test", programlist.toString());


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

我想添加“program_name”的列表字符串,这是粗体标记:

但是我收到了这个错误:

5 个答案:

答案 0 :(得分:1)

@ user3885363。你试试这个。

 JSONArray jsonArray = new JSONArray(response);
        JSONObject jsonObject = jsonArray.getJSONObject(0);
        JSONArray jsonProgramArray = jsonObject.getJSONArray("program_name");

        for (int i = 0; i < jsonProgramArray.length(); i++) {
            JSONObject jsonObjectnew = jsonProgramArray.getJSONObject(i);
            BatchModel programe = new BatchModel();
            programe.setTitle(jsonObjectnew.getString("program_name"));
            programlist.add(programe);
            Log.e("Program test", programlist.toString());


        }

答案 1 :(得分:0)

您在循环jsonObject.length()中使用for (int i = 0; i < jsonObject.length() - 1; i++),但您在对象中获得的数组中只有program_nameJSONArray jsonProgramArray = jsonObject.getJSONArray("program_name");。尝试循环jsonProgramArray并获取每个程序的program_name对象。

答案 2 :(得分:0)

iex(1)> length = 32
32
iex(3)> :crypto.strong_rand_bytes(length) |> Base.encode64 |> binary_part(0, length)
"YiX2oINVqVWCCQZdmESBN44OxcErAFR4"
iex(4)> :crypto.strong_rand_bytes(length) |> Base.encode64 |> binary_part(0, length)
"ka2PSR9cHYSlD6fhdMpnGHgTVA7AoDwN"

您可以对其他batch_name数组执行相同的操作

答案 3 :(得分:0)

你有2&#34; program_name&#34;一个是jsonObject内的字符串&#34; 0&#34;得到你必须这样做

 JSONArray jsonArray = new JSONArray(response);
 JSONObject jsonObject = jsonArray.getJSONObject(0);

 JSONObject jsonObjt_0 = jsonObject.getJSONObject("0");
 String productName = jsonObjt_0.getString("program_name")

然后你有jsonArray名为&#34; program_name&#34;要做到这一点。

JSONArray productArray =  jsonObject.getJSONArray("program_name");
 for (int i = 0; i < productArray .length(); i++) {
   JSONObject listItem = productArray.getJSONObject(i);
   BatchModel programe = new BatchModel();
   programe.setTitle(listItem.getString("program_name"));
   programe.setId(listItem.getString("program_name"));
   programlist.add(programe);
}

PS。这是一个奇怪的json,考虑用一些吝啬的名字改变json,请不要使用相同的名称来做所有事情。

答案 4 :(得分:0)

select pincode from pintable where pincode like '67301%' order by desc;