views:

11860

answers:

2

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

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.

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?

+3  A: 

Hi,

Edit: Thanks Marc, read up on the struct vs class issue and you're right, thank you!

I tend to use the following method for doing what you describe, using a static method of JSON.Net:

MyObject deserializedObject = (MyObject)JavaScriptConvert.DeserializeObject(strJson, typeof(MyObject));

For the Objects list, may I suggest using generic lists out made out of your own small class containing 'attributes' and 'position' class. You can use the Point struct in System.Drawing (System.Drawing.Point or System.Drawing.PointF for floating point numbers) for you X and Y.

After object creation it's much easier to get the data you're after vs. the text parsing you're otherwise looking at.

Cheers, Jarrod

jarrodn
structs would rarely (if ever) be a good choice here; stick to objects (classes).
Marc Gravell
+27  A: 

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);
    }


}
Marc Gravell
+1 for JavaScriptSerializer
roman m
is there a way to convert name value pairs in a JSON string to an existing C# variable type (e.g. Array, or Dictionary?) such that one would not have to create specific/custom classes? In my case the JSON string would be generated in Ruby/Rails...
Greg