如何读取“参数”数组的任何一个

时间:2019-04-09 09:08:18

标签: java android json

在此Json中,如何阅读“参数” Json。请给我一个解决方案,以读取此数据。但是在哪里显示“ params”错误。

  

org.json.JSONException:值[{“ name”:“ Consumer   ID“,” MinLength“:11,” MaxLength“:11,” FieldType“:” NUMERIC“,” IsMandatory“:true}]   类型为java.lang.String的params无法转换为JSONArray

Json

{"statuscode":"TXN","status":"Transaction Successful","data":[{"is_bbps_enabled":1,"payment_amt_exactness":2,"payment_mode":"Cash","payment_channel":"AGT","service_type":"ELECTRICITY","service_provider":"UPPCL (URBAN) - UTTAR PRADESH","product_info":"","provider_key":"UPE","bill_fetch":1,"is_tup":"0","is_down":"0","**params**":"[{\"name\":\"Consumer Number\",\"MinLength\":10,\"MaxLength\":12,\"FieldType\":\"NUMERIC\",\"IsMandatory\":true}]"}]}

我已经尝试过了。

     try {
                    JSONObject jsobobj = new JSONObject(jsonStr);

                    status = jsobobj.getString("statuscode");

                    if(status.equals("TXN")) {

     JSONArray jsonArray = jsobobj.getJSONArray("data");
       for (int i = 0; i < jsonArray.length(); i++ ){
             JSONObject jsonObject = jsonArray.getJSONObject(i);
             JSONArray jsonArrayParam = jsonObject.getJSONArray("params");
               for (int j = 0; j < jsonArrayParam.length(); j++){
                JSONObject jsonObject1 = jsonArrayParam.getJSONObject(j);
                   spKey_Name = jsonObject1.getString("name");

                        }
                      }

try {
                    JSONObject jsobobj = new JSONObject(jsonStr);

                    status = jsobobj.getString("statuscode");

                    if(status.equals("TXN")) {

                      JSONArray jsonArray = jsobobj.getJSONArray("data");
                      for (int i = 0; i < jsonArray.length(); i++ ){
                          JSONObject jsonObject = jsonArray.getJSONObject(i);
                          JSONArray jsonArrayParam = jsonObject.getJSONArray("params");
                          for (int j = 0; j < jsonArrayParam.length(); j++){
                              JSONObject jsonObject1 = jsonArrayParam.getJSONObject(j);
                              spKey_Name = jsonObject1.getString("name");
                              beniname = jsonObject1.getString("name");
                          }
                      }

2 个答案:

答案 0 :(得分:0)

您可以获得这样的“参数”

JSONArray jobjArr=jobj.getJSONArray("data");

    for (int i=0;i<jobjArr.length();i++)
    {
       JSONObject jdata=jobjArr.getJSONObject(i);

       String param=jdata.getString("params");

       JSONArray jParams=new JSONArray(param);

       JSONObject jobjParams=jParams.getJSONObject(0);
       String name=jobjParams.getString("name");

    }

您可以从“名称”之类的对象中获取剩余字符串

答案 1 :(得分:0)

参数String而不是JSONArray。区别在于""之前和之后的[]。您可以从jsonviewer

检查json和验证

您的json

"params":"[{\"name\":\"Consumer Number\",\"MinLength\":10,\"MaxLength\":12,\"FieldType\":\"NUMERIC\",\"IsMandatory\":true}]"

应该是

"params":[{"name":"Consumer Number","MinLength":10,"MaxLength":12,"FieldType":"NUMERIC","IsMandatory":true}]

因此,您有2个选择。第一个是在json中更改以获取JSONArray,第二个是从json中获取String并将其转换为JSONArray

String params = jsonObject.getString("params");
JSONArray jsonArrayParam = new JSONArray(params);