我的Windows Phone应用程序有一个来自JSON(Google Calendar的WebService)的集合。
我可以正确获取JSON,但是什么时候解析,得到一个错误:
Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject. Path", line 1, position 1.
我不知道这是否是问题,但从未使用复杂的JSON。示例:JSON上的“tag”dateTime
位于start
的“内部”,另一个位于end
内部。在我的收藏中,我如何定义这个?
JSON回复:
{
kind: "calendar#events",
etag: ""1416310700455000"",
summary: "desenvolvimentomobile@ff.com.br",
updated: "2014-11-18T11:38:20.455Z",
timeZone: "America/Sao_Paulo",
accessRole: "reader",
defaultReminders: [ ],
nextSyncToken: "CNjoxMGf",
items: [
{
kind: "calendar#event",
etag: ""283f00"",
id: "sf00g1go9399gf",
status: "confirmed",
htmlLink: "https://www.google.com/calendar/event?eid=c2YwMGcxZ285Mzk5Z3YxcTU2Nml2dGl2b3MfiaWxlQGluZmFzc3RlYy5jb20uYnI",
created: "2014-11-17T17:23:05.000Z",
updated: "2014-11-17T17:23:05.362Z",
summary: "Exemplo 1",
creator: {
email: "desenvolvimentomobile@ifc.com.br",
displayName: "Infass silva",
self: true
},
organizer: {
email: "desenvolvimentomobile@ifc.com.br",
displayName: "Infass silva",
self: true
},
start: {
dateTime: "2014-11-17T17:00:00-02:00"
},
end: {
dateTime: "2014-11-17T18:00:00-02:00"
},
iCalUID: "sf00g1go9399fvtivos@google.com",
sequence: 0
}
]}
C#:
DataContext = this;
// String JSON
string json1 = text;
// Parse JObject
JArray jObj1 = JArray.Parse(json1);
Comp = new ObservableCollection<Compromisso>(
jObj1.Children().Select(jo1 => jo1.ToObject<Compromisso>()));
}
}
}
public ObservableCollection<Compromisso> Comp { get; set; }
public class Compromisso
{
public string summary { get; set; }
public string dateTime { get; set; }
public string location { get; set; }
public string description { get; set; }
}
答案 0 :(得分:3)
最简单的方法是使用JSON来粘贴Visual Studio的粘贴功能:
首先,将您的JS对象转换为JSON字符串(例如,使用this tool)。将JSON输出复制到剪贴板并进入Visual Studio中的某个类。使用Edit > Paste Special > Paste JSON As Classes
。
您应该获得以下输出:
public class Rootobject
{
public string kind { get; set; }
public string etag { get; set; }
public string summary { get; set; }
public DateTime updated { get; set; }
public string timeZone { get; set; }
public string accessRole { get; set; }
public object[] defaultReminders { get; set; }
public string nextSyncToken { get; set; }
public Item[] items { get; set; }
}
public class Item
{
public string kind { get; set; }
public string etag { get; set; }
public string id { get; set; }
public string status { get; set; }
public string htmlLink { get; set; }
public DateTime created { get; set; }
public DateTime updated { get; set; }
public string summary { get; set; }
public Creator creator { get; set; }
public Organizer organizer { get; set; }
public Start start { get; set; }
public End end { get; set; }
public string iCalUID { get; set; }
public int sequence { get; set; }
}
public class Creator
{
public string email { get; set; }
public string displayName { get; set; }
public bool self { get; set; }
}
public class Organizer
{
public string email { get; set; }
public string displayName { get; set; }
public bool self { get; set; }
}
public class Start
{
public DateTime dateTime { get; set; }
}
public class End
{
public DateTime dateTime { get; set; }
}
使用以下代码,您可以反序列化它:
MemoryStream jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(text));
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Rootobject));
Rootobject rootObj = ser.ReadObject(jsonStream);
但是,我不确定这是否100%与您的JS对象一起使用,因为etag
不仅被"
包围,而是被""
包围。我从未见过这个。
但是,它应该回答有关如何将JSON映射到类的问题。