Android 读取asp.net webservice的JSON输出
我在asp.net中实现了一个Web服务,它应该以JSON格式返回fortune cookie文本。服务正在运行,我能够使用Web服务的输出。然而,输出并不像预期的那样 WebDevice JSON输出:Android 读取asp.net webservice的JSON输出,android,asp.net,json,web-services,Android,Asp.net,Json,Web Services,我在asp.net中实现了一个Web服务,它应该以JSON格式返回fortune cookie文本。服务正在运行,我能够使用Web服务的输出。然而,输出并不像预期的那样 WebDevice JSON输出: {"d": "{\"CookieText\":\"All you have to know is - what the hell is d ???\"}"} jsonObject = new JSONObject(result); result_intern = jsonObject.get
{"d": "{\"CookieText\":\"All you have to know is - what the hell is d ???\"}"}
jsonObject = new JSONObject(result);
result_intern = jsonObject.getString("d");
正如你可能猜到的,我正在寻找“CookieText”的价值——但我不知道如何获得它。我能够获得以下输出:
{"CookieText":"All you have to know is - what the hell is d ???."}
使用代码(其中result=webevice JSON输出):
但我真正想要的只是CookieText的价值。我尝试了以下几行,但没有成功(result\u intern为空)
我有两个问题
1) 究竟为什么Web服务的输出被打包到变量(无论什么?!)d中?请告诉我,d的目的是什么?大概42岁吧?在我向开发者发送1000封仇恨邮件之前,我真的很想了解它。我没有告诉任何人把它放进d。。。序列化的类中没有d:
public class CFortuneCookieText
{
private string m_CookieText = "";
public string CookieText
{
get { return m_CookieText; }
set { m_CookieText = value; }
}
}
Web服务方法:
[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) 如何使用JSONObject.getString的uage来获取CookieText的值
提前感谢您可以找到第一个问题的答案,这就是为什么web服务使用
d
将数据发送回请求者
对于你的第二个问题,你可以使用“非常著名”来获得你的价值
string result_intern = JsonConvert.DeserializeObject<CFortuneCookieText>(result).CookieText;
您可以找到第一个问题的答案,这就是为什么web服务使用
d
将数据发送回请求者
对于你的第二个问题,你可以使用“非常著名”来获得你的价值
string result_intern = JsonConvert.DeserializeObject<CFortuneCookieText>(result).CookieText;
在我提问的第二部分,我发现了以下我非常满意的解决方法,因为我已经投入了不可接受的时间来运行所有的东西。。。 如果必须在Android中解析ASP.NET webservice字符串,如:
{"d": "{\"CookieText\":\"All you have to know is - what the hell is d ???\"}"}
你所需要的只是CookieText的价值,摆脱全能的“d”:这个小的蹩脚的解决方法可以做到:
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();
}
必须有更好的解决方案,但我很高兴我至少找到了这个;)
感谢Imad帮助我实现了“d”的魔力。在我演讲的第二部分,我发现了以下解决方法,我对此非常满意,因为我已经投入了大量时间来运行所有的东西。。。 如果必须在Android中解析ASP.NET webservice字符串,如:
{"d": "{\"CookieText\":\"All you have to know is - what the hell is d ???\"}"}
你所需要的只是CookieText的价值,摆脱全能的“d”:这个小的蹩脚的解决方法可以做到:
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();
}
必须有更好的解决方案,但我很高兴我至少找到了这个;)
感谢Imad帮助我学习“d”的魔力。+1链接到d的魔力的移植!根据第二个答案:我使用JSONObject类使用android应用程序使用webservice输出,但我不知道如何获取CookieText的值…@GinSonic,你标记了
asp.net
,这就是这样做的方法。检查更新的答案。很抱歉我没有提到android。我不认为这是一个有用的信息,因为我写了我喜欢用JSONObject类获取JSON数据。。。顺便说一句,我在OP中添加了Android。直接调用getString(“CookieText”)不起作用。它导致org.json.JSONException:CookieText异常没有值。+1表示删除d的魔力的链接!根据第二个答案:我使用JSONObject类使用android应用程序使用webservice输出,但我不知道如何获取CookieText的值…@GinSonic,你标记了asp.net
,这就是这样做的方法。检查更新的答案。很抱歉我没有提到android。我不认为这是一个有用的信息,因为我写了我喜欢用JSONObject类获取JSON数据。。。顺便说一句,我在OP中添加了Android。直接调用getString(“CookieText”)不起作用。它导致org.json.JSONException:CookieText异常没有值。