我想让我的json返回,但我很难将它返回到我的班级中,因为它的阵列。但我正在使用我从书中研究的反序列化方法助手。应该工作。
所以我需要回答的问题是我的班级需要如何正确构建才能将json解码为城市对象。
我通过有效的方法抓住我的json
/// <summary>
/// Utility function to get/post WCFRESTService
/// </summary>
/// <param name="methodRequestType">RequestType:GET/POST</param>
/// <param name="methodName">WCFREST Method Name To GET/POST</param>
/// <param name="bodyParam">Parameter of POST Method (Need serialize to JSON before passed in)</param>
/// <returns>Created by David</returns>
private async Task<string> WCFRESTServiceCall(string methodRequestType, string methodName, string bodyParam = "")
{
string ServiceURI = "/launchwebservice/index.php/webservice/" + methodName;
HttpClient httpClient = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(methodRequestType == "GET" ? HttpMethod.Get : HttpMethod.Post, ServiceURI);
if (!string.IsNullOrEmpty(bodyParam))
{
request.Content = new StringContent(bodyParam, Encoding.UTF8, "application/json");
}
HttpResponseMessage response = await httpClient.SendAsync(request);
string jsongString = await response.Content.ReadAsStringAsync();
return jsongString;
}
然后我使用反序列化方法
public static class Helpers
{
public static List<T> Deserialize<T>(this string SerializedJSONString)
{
var stuff = JsonConvert.DeserializeObject<List<T>>(SerializedJSONString);
return stuff;
}
}
我以下列方式呼叫上述内容
string jsonresult = await WCFRESTServiceCall("GET", "cinema_city");
var data = jsonresult.Deserialize<Citys>();
var dialog = new MessageDialog(jsonresult);
await dialog.ShowAsync();
当我检查jsonresult
时,由于某种原因确实返回了json,但是在方括号中。
这给了我以下错误:
我的城市级别如下
public class City
{
public string id { get; set; }
public string timing_title { get; set; }
}
public class Citys
{
public List<City> city { get; set; }
}
编辑以显示杰森数据
{&#34;城市&#34;:[{&#34; ID&#34;:&#34; 5521&#34;&#34; timing_title&#34;:&#34;拉合尔&#34; },{&#34; ID&#34;:&#34; 5517&#34;&#34; timing_title&#34;:&#34;卡拉奇&#34;},{&#34; ID&#34 ;: #&34; 5538&#34;&#34; timing_title&#34;:&#34;伊斯兰堡&#34;},{&#34; ID&#34;:&#34; 5535&#34;&#34 ; timing_title&#34;:&#34;拉瓦尔品第&#34;},{&#34; ID&#34;:&#34; 5518&#34;&#34; timing_title&#34;:&#34;海得拉巴&# 34;},{&#34; ID&#34;:&#34; 5512&#34;&#34; timing_title&#34;:&#34;费萨拉巴特&#34;},{&#34; ID&#34 ;:&#34; 8028&#34;&#34; timing_title&#34;:&#34;古杰朗瓦拉&#34;},{&#34; ID&#34;:&#34; 8027&#34;,& #34; timing_title&#34;:&#34;古吉拉特&#34;}]}
编辑以显示错误 dezerilize事件出错?
答案 0 :(得分:0)
对于你的班级json将是:
对于City class
{ id: "some string", timing_title:"some string" }
和Citys课程:
{ city: [ { id: "some string", timing_title:"some string" }, ... ] }
我相信你的房产名称有错字 - 现在是城市,但真的应该是城市。
我看到的另一个问题是你将Citys类和List混合在一起。它们不一样,你不能将它们序列化/反序列化。
更正List的json:
[ { id: "some string", timing_title:"some string" } ,... ]
将它与上面的Citys课程进行比较。
更新:再次阅读后,您正尝试将列表反序列化为城市。这就是你的例子所说的。可能这就是你想说的:
public static class Helpers
{
public static T Deserialize<T>(this string SerializedJSONString)
{
var stuff = JsonConvert.DeserializeObject<T>(SerializedJSONString);
return stuff;
}
}
string jsonresult = await WCFRESTServiceCall("GET", "cinema_city");
var data = jsonresult.Deserialize<Citys>();
var dialog = new MessageDialog(jsonresult);
await dialog.ShowAsync();