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"}}