栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Json.net解析JSON

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Json.net解析JSON

我不了解JSON.NET,但

JavascriptSerializer
System.Web.Extensions.dll
(.NET 3.5
SP1)中可以正常使用:

using System.Collections.Generic;using System.Web.script.Serialization;public class NameTypePair{    public string OBJECT_NAME { get; set; }    public string OBJECT_TYPE { get; set; }}public enum PositionType { none, point }public class Ref{    public int id { get; set; }}public class SubObject{    public NameTypePair attributes { get; set; }    public Position position { get; set; }}public class Position{    public int x { get; set; }    public int y { get; set; }}public class Foo{    public Foo() { objects = new List<SubObject>(); }    public string displayFieldName { get; set; }    public NameTypePair fieldAliases { get; set; }    public PositionType positionType { get; set; }    public Ref reference { get; set; }    public List<SubObject> objects { get; set; }}static class Program{    const string json = @"{  ""displayFieldName"" : ""OBJECT_NAME"",   ""fieldAliases"" : {    ""OBJECT_NAME"" : ""OBJECT_NAME"",     ""OBJECT_TYPE"" : ""OBJECT_TYPE""  },   ""positionType"" : ""point"",   ""reference"" : {    ""id"" : 1111  },   ""objects"" : [    {      ""attributes"" : {        ""OBJECT_NAME"" : ""test name"",         ""OBJECT_TYPE"" : ""test type""      },       ""position"" :       {        ""x"" : 5,         ""y"" : 7      }    }  ]}";    static void Main()    {        JavascriptSerializer ser = new JavascriptSerializer();        Foo foo = ser.Deserialize<Foo>(json);    }}

编辑:

Json.NET使用相同的JSON和类进行工作。

Foo foo = JsonConvert.DeserializeObject<Foo>(json);

链接:使用Json.NET序列化和反序列化JSON



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/452069.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号