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); 
1
  • its json, not jason. that might help. Commented Nov 30, 2010 at 6:14

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

@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 :(
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)); 

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.