0

I'm using asp.net web api as the server and android as the client. I'm requesting from android to web api and i'm getting json string.When i assign this json string to java object using Gson i'm getting exception.My code is,

private StringBuilder inputStreamToString(InputStream is) {
        String line = "";
        StringBuilder total = new StringBuilder();
        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        // Read response until the end
        try {
            while ((line = rd.readLine()) != null) {
                total.append(line);
            }
            Log.i("RETURN DATA:", total.toString());


              Gson gson = new Gson();
              ReturnData returndata = (ReturnData)
              gson.fromJson( total.toString(), ReturnData.class);
              Log.i("RESULT DATA", returndata.ResultData.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Return full string
        return total;
    }

And my json string(I'm getting from server) like,

"\"OperationResult\":0,\"Messages\":operationsuccess\"\",\"UpdateAvailable\":\"\",\"ResultData\":{\"SessionId\":1584789522,\"UserName\":vinoth,\"AccoundId\":1236985,\"Roles\":[],\"DisplayName\":Vinoth,\"Status\":0,\"Type\":0}}"
6
  • it looks like you are missing opening braces on your response... and why do you have escape characters in the response..? Commented Mar 6, 2013 at 5:48
  • @PrafulBhatnagar I think it is the copy paste problem... Commented Mar 6, 2013 at 5:49
  • and why do you have escape characters in the response..? Commented Mar 6, 2013 at 5:49
  • @PrafulBhatnagar Probably because some libraries like to escape certain characters when serializing. It is within specs Commented Mar 6, 2013 at 5:52
  • What is the exception you get? Commented Mar 6, 2013 at 5:53

2 Answers 2

2

Edit: try this to remove quotes

results is your jsonstring

            if(results!=null && results.length()!=0)
             {
                 if(results.startsWith("\""))
                 {
                     results=results.substring(1,results.length());
                 }
                 if(results.endsWith("\""))
                 {
                     results=results.substring(0,results.length()-1); 
                 }
               }

Your json String has extra escape characters , you need to unescape them you can do it two ways

First and simple one is format the Service in your Server to return pure json.

Second you can handle that string in your java, just download Apache commons lang library and paste the commons-lang3-3.1.jar file to your libs folder

And use it like this

String formatedjsonstring=StringEscapeUtils.unescapeJava(yourjsonstring);
Sign up to request clarification or add additional context in comments.

4 Comments

It is allowed for JSON strings to be escaped in that manner, so the deserializer should handle it.
I used this StringEscapeUtils.unescapeJava(). It very useful for me. But still having one more problem that is couldn't remove the front and rear double quote from the jsonstring.
@VinothKumar updated answer, check it and you can mark this as answer by checking the right mark beside the answer..
It's very useful for me and saved my time. Thanks lot. Now i'm having one more problem now it's not assigning for my java object.I'm assigning the json to (ReturnData returndata) object.If i access the object mean it's showing null.My ReturnData class is,public class ReturnData { public ReturnData() { OperationResult = Result.Failed; Messages = "An Error Occured"; UpdateAvailable = "0"; ResultData = ""; } public Result OperationResult; public String Messages; public String UpdateAvailable; public Object ResultData; }
0

It looks like your json generator on the server is escaping the double quotes that are used as the delimiter for the string... following is the definition of the string from json.org..

enter image description here

The String in the Json should be within double quotes.. escape charter is used when you want to put double quote with in the string...

Following is the valid json if you want to keep quotes as part of the key on json

{
    "\"OperationResult\"": 0,
    "\"Messages\"": "operationsuccess\"\"",
    "\"UpdateAvailable\"": "",
    "\"ResultData\"": {
        "\"SessionId\"": 1584789522,
        "\"UserName\"": "vinoth",
        "\"AccoundId\"": 1236985,
        "\"Roles\"": [],
        "\"DisplayName\"": "Vinoth",
        "\"Status\"": 0,
        "\"Type\"": 0
    }
}

And following is the valid json if you don't want to keep quotes as part of the key..

{
    "OperationResult": 0,
    "Messages": "operationsuccess",
    "UpdateAvailable": "",
    "ResultData": {
        "SessionId": 1584789522,
        "UserName": "vinoth",
        "AccoundId": 1236985,
        "Roles": [],
        "DisplayName": "Vinoth",
        "Status": 0,
        "Type": 0
    }
}

There is this very beautiful and simple site called JSONLint.com that I always use to check if the json is right or not..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.