简体   繁体   中英

Android JSON parsing Json Array is [] Throws Null Pointer Exception while parsing, How to write in Proper way?

I have complex API that i parse and show in list view,I will be struggle to parse JSONArray.Here i will be in struggle following Json Array which is inside the posts json object "tags_name": ["Activities"], ,some object it will come like "tags_name": [], this.Kindly review my question. My API and code is below. Presently i will implemented parsing code with model class. Once fix this issue i have to write list view coding please help me. May be my question formation is in silly way. please it look like means give some suggestion to frame question. Thanks in Advance.

MyAPI:

    {
    "status": true,
    "nextpage": 0,
    "count": 31,
    "data": {
        "postlist": [{
                "posts": {},
                "tags_name": ["Activities"],
                "images_count": 3,
                "images": [],
                "post_user": [],
                "is_encourage_user": true,
                "encourage_feed_id": "1093"
            },

            {
                "posts": {"id": "4647"},
                "tags_name": [],
                "images_count": 0,
                "images": [],
                "post_user": [],
                "is_encourage_user": true,
                "encourage_feed_id": "992"
            }

        ]
},
    "token": "wqeeqweqweqweqweqsfsdfsdf"
    }

My Method for Parsing

private void parsingPostValues(String responseStatus) throws JSONException {
        JSONObject responseObject = new JSONObject(responseStatus);
        JSONObject datObject = responseObject.getJSONObject("data");
        JSONArray postArr = new JSONArray(datObject.getString("postlist"));
        for (int i = 0; i < postArr.length(); i++) {
            JSONObject tempPostObject = postArr.getJSONObject(i);
            JSONObject postObject = tempPostObject.getJSONObject("posts");

            //setTag Array- this is the functional area i'm in bottle-neck.
            try {
                JSONArray tagNameArr = tempPostObject.getJSONArray("tags_name");
                //ArrayList<Bean> tagListdata = new ArrayList<Bean>(tagNameArr.length());
                if (tagNameArr.length()>0) {
                    for (int tagInfo = 0; tagInfo < tagNameArr.length(); tagInfo++) {
                        // listdata.add(tagNameArr.get(i).toString());
                        String tagme = "";
                        //Bean tagBean = new Bean();
                        //tagBean.setTagsArray((tagme.isEmpty() ? tagNameArr.get(tagInfo).toString() : "null"));                            //tagBean.setTagsArray(tagNameArr.get(tagInfo).toString());
                        //tagListdata.add(tagBean);
                        //beanAccess.setTagsArray(tagNameArr.get(tagInfo));
                        System.out.println("Tags Array:"+tagInfo+":"+tagNameArr.get(tagInfo));
                    }
                    //beanAccess.setTagsArray(tagListdata);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

    }

replace this

JSONArray postArr = new JSONArray(datObject.getString("postlist"));

To

JSONArray postArr = datObject.getJSONArray("postlist");

Replace

String imgCount = tempPostObject.getString("images_count");
String is_encourage_user = tempPostObject.getString("is_encourage_user");

To

String imgCount = postObject.getString("images_count");
String is_encourage_user = postObject.getString("is_encourage_user");

It will work for you.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM