Kishore Kishore - 1 year ago 112
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 Source

First use this:

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 :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download