【问题标题】:How work with JSON correctly如何正确使用 JSON
【发布时间】:2014-11-18 16:55:12
【问题描述】:

我的 Windows Phone 应用有一个 JSON(Google 日历的 Web 服务)的集合。

我可以正确获取 JSON,但是 Parse 什么时候出现错误: 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; }
    }

【问题讨论】:

    标签: c# json windows-phone


    【解决方案1】:

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

    首先,将您的 JS 对象转换为 JSON 字符串(例如,使用this tool)。将 JSON 输出复制到剪贴板并进入 Visual Studio 中的某个类。使用Edit &gt; Paste Special &gt; 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 映射到类的问题。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 2016-04-06
    • 2016-06-04
    • 2013-05-12
    相关资源
    最近更新 更多