0

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

GinSonic
  • 70
  • 9

2 Answers2

3

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");
Community
  • 1
  • 1
Imad
  • 7,126
  • 12
  • 55
  • 112
  • +1 for the link to an expantation of the magic of d! According the second answer: I consume the webservice output with an android app using the JSONObject Class and i can´t figure out how to get the value for CookieText... – GinSonic Oct 21 '16 at 11:52
  • @GinSonic You tagged `asp.net` and this is the way to do it in that. Check updated answer. – Imad Oct 21 '16 at 12:23
  • Sorry that i didnt mention android. I didnt think that it was a useful information since i wrote that i like to get the JSON data with the JSONObject Class... Anayway, I added Android to the OP. The direct call of getString("CookieText") does not work. It causes an org.json.JSONException: No value for CookieText exception. – GinSonic Oct 21 '16 at 12:39
0

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".

GinSonic
  • 70
  • 9