使用 Json.net 解析 JSON [英] Parsing JSON using Json.net

查看:41
本文介绍了使用 Json.net 解析 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 JSon.Net 库解析一些 JSON.文档似乎有点稀疏,我对如何完成我需要的东西感到困惑.这是我需要解析的 JSON 格式.

I'm trying to parse some JSON using the JSon.Net library. The documentation seems a little sparse and I'm confused as to how to accomplish what I need. Here is the format for the JSON I need to parse through.

{
    "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
        }
    } ]
}

我真正需要的唯一数据是对象数组中的内容.我是否可以使用 JSonTextReader 之类的东西解析它,然后取出我想要的东西,比如 OBJECT_TYPE 和 x 和 y 位置?我似乎无法让 JSonTextReader 以我想要的方式工作,而且我几乎没有发现它的使用示例.

The only data I really need from this is the stuff in the objects array. Is it possible for me to parse through that with something like the JSonTextReader and just pull out the things I want, like OBJECT_TYPE and the x and y position? I can't seem to get JSonTextReader to work the way I want it to and I find little to no examples of usage for it.

似乎先序列化然后将 LINQ 与我的对象一起使用是理想的,我找到的每个示例都首先讨论了序列化 JSON,但我不确定如何为该结构构建对象.特别是对象数组,它需要类似于属性和位置对象对的列表.我不知道如何编码我的对象,以便 JSon.Net 知道如何序列化它.

It seems like serializing first then using LINQ with my object would be ideal and every example I find discusses serializing the JSON first, but I'm not sure how I would build an object for this structure. Particularly the objects array which would need to be something like a list of Pairs of attribute and position objects. I have no idea how I would code my object so JSon.Net would know how to serialize that.

我以为我可以编写自己的简单解析器,将我需要的所有内容提取到我创建的属性对象中,但我运气不佳.

I thought I could write my own simple parser to just pull out everything I need into an attributes object that I created, but I'm having little luck.

希望这一切都有意义,有什么想法吗?

Hopefully this all makes sense, any ideas?

推荐答案

我不知道 JSON.NET,但它与 System.Web.Extensions 的 JavaScriptSerializer 一起工作得很好.dll (.NET 3.5 SP1):

I don't know about JSON.NET, but it works fine with JavaScriptSerializer from 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 和类.

Json.NET works using the same JSON and classes.

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

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

这篇关于使用 Json.net 解析 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆