2

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);
Patrick
  • 1,629
  • 5
  • 23
  • 44
Kishore
  • 2,051
  • 5
  • 26
  • 45

3 Answers3

2

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

ankitjaininfo
  • 11,961
  • 7
  • 52
  • 75
Patrick Boos
  • 6,789
  • 3
  • 35
  • 36
  • @Ankit Jain: thx for the edit. finally I know how I can make those links correct :) [linkurl][1] it is :) but no.. why can't i do it this way in posts? it won't accept that format :( – Patrick Boos Nov 30 '10 at 07:20
1

I've found jackson's ObjectMapper to be extremely helpful in cases like this.

ObjectMapper objectMapper = new ObjectMapper();
clsError error = objectMapper.readValue(temp, clsError.class));
burtyish
  • 1,033
  • 2
  • 13
  • 27
0

You can use jQuery extend method to wrap your json into javascript object as below:

function clsError(ErrorCode,ErrorDescription,UserID){
   this.ErrorCode=ErrorCode;
   this.ErrorDescription=ErrorDescription;
   this.UserID=UserID;
}

String temp="{"clsError":{"ErrorCode":110,"ErrorDescription":" Request Added"},"UserID":36}"
var obj = $.extend(new clsError(), temp);
uzay95
  • 16,052
  • 31
  • 116
  • 182