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

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

问题描述

我试图解析使用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,但我不知道我会怎样构建对象为这种结构。特别的对象阵列将需要像属性和位置的对象的对的列表。我不知道我怎么会code我的对象,以便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,但它正常工作与的JavaScriptSerializer System.Web.Extensions.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);

链接:<一个href=\"http://james.newtonking.com/projects/json/help/index.html?topic=html/SerializingJSON.htm\">Serializing和反序列化JSON与Json.NET

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

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