GinSonic
GinSonic

Reputation: 80

Read JSON output of asp.net webservice

I implemented a webservice in asp.net which should return fortune cookie text in JSON format. The service is working and i´m able to consume the output of the webservice. However, the output is not as expected...

Websevice JSON output:

{"d": "{\"CookieText\":\"All you have to know is - what the hell is d ???\"}"}

As you might guess, i´m looking for the value of "CookieText" - but i don´t know how to get that. I´m able to get the following output:

{"CookieText":"All you have to know is - what the hell is d ???."}

with the code (where result = Websevice JSON output):

jsonObject = new JSONObject(result);
result_intern = jsonObject.getString("d");

But what i´m really looking for is just the value of CookieText. I tried the following lines with no success (result_intern is empty).

 jsonObject = new JSONObject(result);
 result_intern = jsonObject.getJSONObject("d").getString("CookieText");

I have two questions.

1) Why in the world is the output of the webservice packed into the variable (whatever?!) d ? Please enlighten me, what is the purpose of d? Is it something like 42? I really like to understand it before i send 1000s of hate mails to the developers. I did not tell anyone to put it into d... There is no d in the serialized class:

public class CFortuneCookieText
{
    private string m_CookieText = "";

    public string CookieText
    {
        get { return m_CookieText; }
        set { m_CookieText = value; }
    }
}

Webservice method:

[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 
    public string GetFortuneCookieSayingsJSON()
    {
        CFortuneCookieText tmpObj   = new CFortuneCookieText();
        tmpObj.CookieText           = "All you have to know is - what the hell is d ???";

        return JsonConvert.SerializeObject(tmpObj, Formatting.None);
    }

2) How do i get just the value of CookieText with the uage of JSONObject.getString?

Thanks in advance

Upvotes: 0

Views: 68

Answers (2)

GinSonic
GinSonic

Reputation: 80

For the second part of my qusetion i found the following workaround with which i´m quite happy with because i already invested an unacceptable amount of time to get all the things running... If you have to parse an ASP.NET webservice string within Android like:

{"d": "{\"CookieText\":\"All you have to know is - what the hell is d ???\"}"}

Where all you need is the value of CookieText and get rid of the allmighty "d": this little crappy workaround does it:

    try
    {
        JSONObject jsonObject   = new JSONObject(JSON_String);
        tmp_result              = jsonObject.getString("d");
        JSONObject tmpJSONObj   = new JSONObject(tmp_result);
        result                  = tmpJSONObj.getString("CookieText");

    } 
    catch (JSONException e)
    {
        e.printStackTrace();
    }

There has to be a better solution, but i´m glad i at least found this one ;) Thanks Imad for helping me with the magic of "d".

Upvotes: 0

Imad
Imad

Reputation: 7490

You can find answer to your first question here that is why web services use d to send data back to requester.

for your second question, you can use very famous Json.net and get your value as

string result_intern = JsonConvert.DeserializeObject<CFortuneCookieText>(result).CookieText;

For android you can directly call getString on response as

result_intern = jsonObject.getString("CookieText");

Upvotes: 3

Related Questions