如何正确使用 JSON

本文关键字:JSON 何正确 | 更新日期: 2023-09-27 18:31:02

我的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 上的"标签"dateTimestart的"内部",另一个在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; }
    }

如何正确使用 JSON

最简单的方法是使用 Visual Studio 的 JSON 到类粘贴功能:

首先,将JS对象转换为JSON字符串(例如,使用此工具)。将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 映射到类的问题。