0

下面是我从 WebService 响应中得到的给定字符串。

{
    "tag": "fetchdwnDetailStatus",
    "status": true,
    "downloadDetails": "[\"[9164666666, 20171001, MADAN LAL CHOURISYA, 16, 1342.6]\",\"[9868476619, 20171001, RAHUL JAIN\\\/CHUNNILAL JAIN, 50, 121]\",\"[9173666666, 20171001, SHRI BALKRISHAN \\\/ DHARMLAL KUR, 13, 3246]\",\"[9833216286, 20171001, SMT CHANDA JAIN\\\/SHRI CHANDMAL , 50, 4502.23]\",\"[7073016885, 20171001, MAHESH KUMAR \\\/ BRAJLAL KUSHWAH, 48, 1560.8]\"]"
}

我想遍历 的每个元素downloadDetails,并希望在屏幕上显示。

所以,到目前为止我做到了

JSONObject jObj = new JSONObject(response);
if(jObj!=null)
{                         
    arrayFromStringDD = jObj.getJSONArray("downloadDetails");
    try {        
            JSONArray jsonArray = arrayFromStringDD;
            List<String> list = new ArrayList<String>();
            for (int j=0; j<jsonArray.length(); j++) {
               System.out.println("adding in list==>"+ jsonArray.getString(j));
                list.add( jsonArray.getString(j) );
            } 
     }catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }                          
}

我收到的不是原始类型错误arrayFromStringDD = jObj.getJSONArray("downloadDetails"); 请帮助

编辑:正如帖子中所建议的,这是字符串而不是数组。所以我尝试了这个。

JsonParser jsonParser = new JsonParser();
JsonArray arrayFromStringDD = jsonParser.parse((String) jObj.getString("downloaddata")).getAsJsonArray();
 try {        
            JSONArray jsonArray = arrayFromStringDD; //here I am getting error while converting...
            List<String> list = new ArrayList<String>();
            for (int j=0; j<jsonArray.length(); j++) {
               System.out.println("adding in list==>"+ jsonArray.getString(j));
                    list.add( jsonArray.getString(j) );
                } 
             }catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
4

4 回答 4

0

改造库将使您的生活更轻松。它支持 JSON 到 java 对象的转换。 http://square.github.io/retrofit/

于 2018-02-06T13:23:33.413 回答
0

downloadDetails的类型是字符串而不是数组,因为它包含在“”中。因此,当您尝试将 String 转换为 ArrayObject 时,您会收到错误

查看下面的示例并将其与您的 json 进行比较:

{
"name":"John",
"age":30,
"cars":[ "Ford", "BMW", "Fiat" ]
} 
于 2018-02-06T13:35:40.300 回答
0

试试这个

JSONObject jObj = new JSONObject(response);
if(jObj!=null)
{                         

try 
{

     String downloadDetails = jObj.getString("downloadDetails");

     String jsonFormattedString = downloadDetails.replaceAll("\\\\", "");

     JSONArray jsonArray = new JSONArray(jsonFormattedString);

     for (int i = 0; i < jsonArray.length(); i++) {

         System.out.println("adding in list==>"+ jsonArray.getString(i));
     }

}
catch (JSONException e) 
{
      // TODO Auto-generated catch block
      e.printStackTrace();
}

输出

在此处输入图像描述

编辑

JSONObject jObj = new JSONObject(response);
if(jObj!=null)
{                         

try 
{

     String downloadDetails = jObj.getString("downloadDetails");

     String jsonFormattedString = downloadDetails.replaceAll("\\\\", "");

     JSONArray jsonArray = new JSONArray(jsonFormattedString);

     for (int i = 0; i < jsonArray.length(); i++) {

         System.out.println("adding in list==>"+ jsonArray.getString(i));

         String innerArray = jsonArray.getString(i);


         innerArray = innerArray.replace("[","");
         innerArray = innerArray.replace("]","");

         String[] innerValue = innerArray.split(",");

         for (int j = 0; j < innerValue.length; j++){

           Log.i("OUTPUT",":"+innerValue[j]);

         }
     }

}
catch (JSONException e) 
{
      // TODO Auto-generated catch block
      e.printStackTrace();
}

内部输出

I/adding in list==>: [9164666666, 20171001, MADAN LAL CHOURISYA, 16, 1342.6]
I/OUTPUT: :9164666666
I/OUTPUT: : 20171001
I/OUTPUT: : MADAN LAL CHOURISYA
I/OUTPUT: : 16
I/OUTPUT: : 1342.6
于 2018-02-06T14:05:36.590 回答
0

我想在@Niket Shah 的回答中添加我的评论

try {

            JSONObject jObj = new JSONObject(text);
            String arrayString = jObj.getString("downloadDetails");

            JSONArray jObjTemp = new JSONArray(arrayString);

            List<String> list = new ArrayList<String>();
            for (int j = 0; j < jObjTemp.length(); j++) {
                System.out.println("adding in list==>" + jObjTemp.getString(j));

                list.add(jObjTemp.getString(j));
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

当我得到日志时,它显示:

I/System.out: 添加列表==>[9164666666, 20171001, MADAN LAL CHOURISYA, 16, 1342.6] I/System.out: 添加列表==>[9868476619, 20171001, RAHUL JAIN/CHUNNILAL JAIN, 50, 121] I/System.out:添加列表 ==> [9173666666, 20171001, SHRI BALKRISHAN / DHARMLAL KUR, 13, 3246]

I/System.out: 添加列表==>[9833216286, 20171001, SMT CHANDA JAIN/SHRI CHANDMAL , 50, 4502.23]

I/System.out: 添加列表==>[7073016885, 20171001, MAHESH KUMAR / BRAJLAL KUSHWAH, 48, 1560.8]

如果您有疑问,请随时发表评论。

快乐的编码...

于 2018-02-06T14:07:13.117 回答