如何在Windows Phone 7中实现Json?可以';t使用动态关键字
本文关键字:关键字 动态 可以 Windows Phone Json 实现 | 更新日期: 2023-09-27 18:21:14
我正在使用wp7,即使当我在VS中键入"dynamic"时,它也会突出显示并允许我编译和运行应用程序,但一旦我尝试使用它,就会出现编译错误。
我读到我不能使用dynamic关键字,现在有点迷失在如何进行json解析上(我使用json.net和restsharp,但如果我不能使用动态,两者都会遇到相同的问题)
例如,如果我使用foursquare api。所有json数据总是像一样返回
{
"meta": {
"code": 200,
...errorType and errorDetail...
},
"notifications": {
...notifications...
},
"response": {
...results...
}
}
但是响应将具有不同的数据。例如,它可能有用户数据(用户类),也可能有场地数据(场地类)。
但最重要的是,我需要一个名为Response的类,它位于RootObject类中。
但我不能有相同的类名(除非我开始把它们放在不同的名称空间里,但又不为这个想法疯狂)。
我能想到的唯一糟糕的事情就是
public class RootObject
{
public Response Response { get; set; }
}
public class Response
{
public User User { get; set; }
public List<Venue> Venues { get; set; }
}
这个响应对象最终将具有所有可能返回的不同类,并且实际上可能只填充响应对象中的属性。
有更好的方法吗?
好吧,我花了很长时间才得到一个样本(我讨厌OAuth),但您可以使用带有接口的JsonConverter
来解析响应。
参见:
- JSON.NET中用于反序列化的强制转换接口
以下是用户和场馆的示例:
转换器:
public class ResponseConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(IResponse));
}
public override object ReadJson(JsonReader reader,
Type objectType,
object existingValue,
JsonSerializer serializer)
{
// reader is forward only so we need to parse it
var jObject = JObject.Load(reader);
if(jObject["user"] != null)
{
var user = jObject.ToObject<UserResponse>();
return user;
}
if(jObject["venues"] != null)
{
var venue = jObject.ToObject<VenuesResponse>();
return venue;
}
throw new NotImplementedException("This reponse type is not implemented");
}
public override void WriteJson(JsonWriter writer,
object value,
JsonSerializer serializer)
{
// Left as an exercise to the reader :)
throw new NotImplementedException();
}
}
DTO:
public class ApiRespose
{
public ApiRespose()
{
Notifications = new List<Notification>();
}
[JsonProperty("meta")]
public Meta Meta { get; set; }
[JsonProperty("notifications")]
public List<Notification> Notifications { get; set; }
[JsonProperty("response")]
[JsonConverter(typeof(ResponseConverter))]
public IResponse Response { get; set; }
}
public interface IResponse
{
}
public class UserResponse : IResponse
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("firstname")]
public string FirstName { get; set; }
[JsonProperty("lastname")]
public string LastName { get; set; }
// Other properties
}
public class VenuesResponse : IResponse
{
public VenuesResponse()
{
Venues = new List<Venue>();
}
[JsonProperty("venues")]
public List<Venue> Venues { get; set; }
}
public class Venue
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
// Other Properties
}
public class Meta
{
[JsonProperty("code")]
public int Code { get; set; }
}
public class Notification
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("item")]
public Item Item { get; set; }
}
public class Item
{
[JsonProperty("unreadcount")]
public int UnreadCount { get; set; }
}
用法:
string jsonData = GetJsonData();
var apiResponse = JsonConvert.DeserializeObject<ApiResponse>(json);