如何解析JSON以便我可以遍历元素

时间:2018-02-06 13:20:31

标签: android json jsonparser

以下是我从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 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

{
"name":"John",
"age":30,
"cars":[ "Ford", "BMW", "Fiat" ]
} 

答案 2 :(得分: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();
}

<强>输出

enter image description here

修改

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

答案 3 :(得分: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:在列表中添加==&gt; [9164666666,20171001,MADAN LAL CHOURISYA,16,1342.6]   I / System.out:在列表中添加==&gt; [9868476619,20171001,RAHUL JAIN / CHUNNILAL JAIN,50,121]   I / System.out:在列表中添加==&gt; [9173666666,20171001,SHRI BALKRISHAN / DHARMLAL KUR,13,3246]

     

I / System.out:在列表中添加==&gt; [9833216286,20171001,SMT CHANDA JAIN / SHRI CHANDMAL,50,4502.23]

     

I / System.out:在列表中添加==&gt; [7073016885,20171001,MAHESH KUMAR / BRAJLAL KUSHWAH,48,1560.8]

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

快乐编码......