My problem is that my android app does not parse the following JSON data. If I use other JSON sources it parses OK. I've validated the JSON bellow and there were no problems. It doesn't throw any error. I can't understand why.
JSON data looks like this:
[
{
"Id":"1",
"name":"habib",
"f":[
{
"Id":"1",
"taeed":"0",
"codkala":"101",
"namekala":"سس گوجه فرنگی",
"count":"5",
"chid":"0"
},
{
"Id":"1",
"taeed":"0",
"codkala":"102",
"namekala":"ماست موسیر",
"count":"1",
"chid":"0"
},
{
"Id":"1",
"taeed":"0",
"codkala":"103",
"namekala":"شیر گم چرب",
"count":"3",
"chid":"0"
}
]
},
{
"Id":"2",
"name":"habib",
"f":[
{
"Id":"2",
"taeed":"0",
"codkala":"101",
"namekala":"سس گوجه فرنگی",
"count":"5",
"chid":"0"
},
{
"Id":"2",
"taeed":"0",
"codkala":"102",
"namekala":"ماست موسیر",
"count":"1",
"chid":"0"
},
{
"Id":"2",
"taeed":"0",
"codkala":"103",
"namekala":"شیر گم چرب",
"count":"3",
"chid":"0"
}
]
}
]
And my code for parsing:
public class AnbarParser {
public static List<AnbarModel> parseFeedAnbar(String content){
try {
JSONArray ar = new JSONArray(content);
List<AnbarModel> anbarModelList = new ArrayList<>();
for (int i = 0; i<1; i++){
JSONObject obj = ar.getJSONObject(i);
AnbarModel anbarModel = new AnbarModel();
anbarModel.setId(obj.getString("Id"));
anbarModel.setName(obj.getString("name"));
List<Faktor> faktorlist = new ArrayList<>();
JSONArray arr2 = obj.getJSONArray("f");
for (int ii = 0; i<1; ii++){
JSONObject obj2 = arr2.getJSONObject(i);
Faktor faktor = new Faktor();
faktor.setId(obj2.getString("Id"));
faktor.setTaeed(obj2.getString("taeed"));
faktor.setCodkala(obj2.getString("codkala"));
faktor.setNamekala(obj2.getString("namekala"));
faktor.setCount(obj2.getString("count"));
faktor.setChid(obj2.getString("chid"));
faktorlist.add(faktor);
}
anbarModel.setFaktor(faktorlist);
anbarModelList.add(anbarModel);
}
return anbarModelList;
}catch (JSONException e){
e.printStackTrace();
return null;
}
//return null;
}
}
Hope you missed a small " i
" in you array for loop.
Since i is greater than 1 because of its previous loop, it will never come inside the for loop for array. change your code link below.
check the below code.
for (int ii = 0; ii<1; ii++){ // here is the only change. add ii instead of i
JSONObject obj2 = arr2.getJSONObject(ii); // here also replace your i with ii
Faktor faktor = new Faktor();
faktor.setId(obj2.getString("Id"));
faktor.setTaeed(obj2.getString("taeed"));
faktor.setCodkala(obj2.getString("codkala"));
faktor.setNamekala(obj2.getString("namekala"));
faktor.setCount(obj2.getString("count"));
faktor.setChid(obj2.getString("chid"));
faktorlist.add(faktor);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.