[英]Deserialize list of JSON objects
I got a Windows Phone 8.1 app. 我有一个Windows Phone 8.1应用程序。
I am trying to parse a list of objects returned by my Web API call. 我正在尝试解析Web API调用返回的对象列表。
On my server I have this code: 在我的服务器上,我有以下代码:
Default.aspx
: Default.aspx
:
[WebMethod]
public static List<Shared.PremisesResponse> GetPremises(string emailaddress)
{
Premises premises = new Premises();
return premises.GetPremises(emailaddress);
}
In that premise object class 在那个前提下的对象类
public List<Shared.PremisesResponse> GetPremises(string emailAlias)
{
DAL dal = new DAL();
List<Shared.PremisesResponse> premises = new List<Shared.PremisesResponse>();
try
{
DataSet dtMacs = dal.GetMacs(emailAlias);
for (int index = 0; index < dtMacs.Tables[0].Rows.Count; index++)
{
Shared.PremisesResponse itemMAC1 = new Shared.PremisesResponse();
itemMAC1.PremiseName = dtMacs.Tables[0].Rows[index]["PremiseName"].ToString().Trim();
itemMAC1.Alias = dtMacs.Tables[0].Rows[index]["Alias"].ToString().Trim();
premises.Add(itemMAC1);
}
}
catch (Exception ex)
{
Email2.SendError("Premises.GetPremises:" + ex.ToString(), emailAlias);
Shared.PremisesResponse itemMAC1 = new Shared.PremisesResponse();
itemMAC1.PremiseName = "ERROR";
itemMAC1.Alias = ex.ToString();
premises.Add(itemMAC1);
}
return premises;
}
The class Shared.Premise
: 类Shared.Premise
:
public class PremisesResponse
{
public string PremiseName;
public string Alias;
}
In my WP8.1 client app: 在我的WP8.1客户端应用程序中:
public async static Task<List<D2>> GetPremises( string emailaddress)
{
List<D2> premises = new List<D2>();
try
{
using (var client = new HttpClient())
{
var resp = await client.PostAsJsonAsync("http://my url/NativeApp/Default.aspx/GetPremises",
new { emailaddress = emailaddress });
var str = await resp.Content.ReadAsStringAsync();
var premisesResponse = JsonConvert.DeserializeObject<List<D2>>(str);
foreach (var pr in premisesResponse)
{
D2 d2 = new D2();
d2.Alias = pr.Alias;
d2.PremiseName = pr.PremiseName;
premises.Add(d2);
}
}
}
catch (Exception ex)
{
//evMessage(Enums.MessageType.Error, serverRegister);
}
return premises;
}
And the objects I am using in my client: 以及我在客户端中使用的对象:
public class D2
{
public string __type { get; set; }
public string PremiseName;
public string Alias;
}
public class PremisesResponse
{
public D2 d { get; set; }
}
'var str' returns this value: 'var str'返回此值:
{"d":[{"__type":"InformedMotionBiz.Shared+PremisesResponse","PremiseName":"Informatica 2000","Alias":"9A5C3-E1945-3D315-BB43C"},{"__type":"InformedMotionBiz.Shared+PremisesResponse","PremiseName":"My Office","Alias":"40387-69918-FC22F-C444B"}]}
The error occurs on this line: 该行发生错误:
var premisesResponse = JsonConvert.DeserializeObject<List<PremisesResponse>>(str);
The error message is: 错误消息是:
[Informed.D2]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. [Informed.D2]',因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。 To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. 要解决此错误,可以将JSON更改为JSON数组(例如[1,2,3]),也可以更改反序列化类型,使其成为普通的.NET类型(例如,不像整数这样的原始类型,也不像这样的集合类型)数组或列表),可以从JSON对象反序列化。 JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. 还可以将JsonObjectAttribute添加到类型中,以强制其从JSON对象反序列化。
Path 'd', line 1, position 5. 路径“ d”,第1行,位置5。
I have no problems returning a single value, just with returning a list of this object. 我没有问题返回单个值,只需返回此对象的列表即可。
Any pointers would be helpful. 任何指针都会有所帮助。
Thanks 谢谢
You're trying to deserialize a List<D2>
rather than a single D2
object: 您正在尝试反序列化List<D2>
而不是单个D2
对象:
public class PremisesResponse
{
public D2[] d { get; set; }
}
And then simply: 然后简单地:
PremisesResponse response = JsonConvert.DeserializeObject<PremisesResponse>(str);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.