0

I am working in web service.Now I want to take values from json object,so I create one parser class and pass the json object.In System.out.println("main object"+mainObj) I got the json object.But in if(mainObj!=null) I got false.so the remaining code is not working.Here return type is always null.

Following is my code,

Parser Class

public class JSONParser {

    private Context mContext;
    public JSONParser(Context context)
    {
        mContext=context;
    }
    public JSON_Parser_Registration[] ObjectReturner(String result)
    {
        JSON_Parser_Registration[] mJSONObj=null;
        try
        {
            JSONObject mainObj = new JSONObject(result);
            System.out.println("main object"+mainObj);
            if(mainObj!=null)
            {
                JSONArray list = mainObj.getJSONArray("result");
                System.out.println("list values"+list);
                if(list != null)
                {
                    mJSONObj=new JSON_Parser_Registration[list.length()];                       
                    for(int i = 0; i < list.length();i++)
                    {
                        JSONObject elem = list.getJSONObject(i);
                        System.out.println("element values"+elem);
                        if(elem != null)
                        {
                            String id=elem.getString("id");
                            String msg=elem.getString("msg");
                            mJSONObj[i]=new JSON_Parser_Registration();
                            mJSONObj[i].SetValues(id, msg);
                            System.out.println("id is "+id +" \nMessage is is "+msg);

                        }
                    }
                }
            }

        }
        catch(Exception e)
        {

        }
        return mJSONObj;
    }

}

JSON_Parser_Registration class

public class JSON_Parser_Registration {

    public String mId;
    public String mMessage;


    public void SetValues(String id,String msg)
    {
        mId=id;
        mMessage=msg;

    }
}

The JSON Object is in this format from server

{"result":{"id":248,"msg":"AppUser added successfully"}}
Vigbyor
  • 2,568
  • 4
  • 21
  • 35
user3122468
  • 39
  • 1
  • 6

3 Answers3

2

result is a JSONObject not a JSONArray.

This

JSONArray list = mainObj.getJSONArray("result");

is wrong.

{ // json object node
    "result": { // jsonobject result
        "id": 248, 
        "msg": "AppUser added successfully"
    }
}

So it should be

JSONObject job = mainObj.getJSONObject("result");
String id=job.getInt("id");
String msg=job.getString("msg")
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

use jackson library. Here is the example you are looking for

How to use Jackson to deserialise an array of objects

Why to reinvent the wheel :)

Community
  • 1
  • 1
M Sach
  • 33,416
  • 76
  • 221
  • 314
0

Use this code

 JSONObject elem =new JSONObject(list.getJSONObject(i));
    if(elem != null)
    {
              String id=elem.getString("id");
              String msg=elem.getString("msg");
              mJSONObj[i]=new JSON_Parser_Registration();
              mJSONObj[i].SetValues(id, msg);
              System.out.println("id is "+id +" \nMessage is is "+msg);
    }
learner
  • 3,092
  • 2
  • 21
  • 33