Kishore Kishore - 6 months ago 27
Android Question

how to convert json object into class object

How to convert this json clsError and userId values:

String temp = "{"clsError":{"ErrorCode":110,"ErrorDescription":" Request Added"},"UserID":36}"


And pass them as parameters:

clsError errorObject=new clsError(UserID,clsError);

Answer

First use this: http://developer.android.com/reference/org/json/JSONObject.html#JSONObject(java.lang.String)

JSONObject tempJson = new JSONObject(temp);
JSONObject clsErrorJson = tempJson.getJSONObject("clsError");
clsError errorObject = new clsError(tempJson.getString("UserID"),
                                    clsErrorJson.getInt("clsError") + ": "
                                    + clsErrorJson.getString("ErrorDescription"));

that is basically it. but i was not sure about the second parameter of the clsError constructor. i just assumed it is a string in my example :) but this should give you the idea how to do this. and you will have to surround that with try/catch. eclipse will tell you how :)