下面是我从 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();
}