Difference between revisions of "CSharp - Using JSON"
Jump to navigation
Jump to search
Google
PeterHarding (talk | contribs) (Created page with "=Links= * http://stackoverflow.com/questions/401756/parsing-json-using-json-net Category:CSharp Category:JSON") |
PeterHarding (talk | contribs) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
* http://stackoverflow.com/questions/401756/parsing-json-using-json-net | * http://stackoverflow.com/questions/401756/parsing-json-using-json-net | ||
* http://mono.servicestack.net/mythz_blog/?p=344 | |||
* [http://mono.servicestack.net/benchmarks/NorthwindDatabaseRowsSerialization.100000-times.2010-08-17.html Benchmarks of the leading JSON serializers] | |||
* http://www.codeproject.com/Tips/397574/Use-Csharp-to-get-JSON-Data-from-the-Web-and-Map-i | |||
=Google= | |||
See - http://stackoverflow.com/questions/1474377/json-library-for-c-sharp | |||
* http://mono.servicestack.net/ | |||
<pre> | |||
using System.Collections.Generic; | |||
using System.Web.Script.Serialization; | |||
namespace GoogleTranslator.GoogleJSON | |||
{ | |||
public class FooTest | |||
{ | |||
public void Test() | |||
{ | |||
const string json = @"{ | |||
""DisplayFieldName"" : ""ObjectName"", | |||
""FieldAliases"" : { | |||
""ObjectName"" : ""ObjectName"", | |||
""ObjectType"" : ""ObjectType"" | |||
}, | |||
""PositionType"" : ""Point"", | |||
""Reference"" : { | |||
""Id"" : 1111 | |||
}, | |||
""Objects"" : [ | |||
{ | |||
""Attributes"" : { | |||
""ObjectName"" : ""test name"", | |||
""ObjectType"" : ""test type"" | |||
}, | |||
""Position"" : | |||
{ | |||
""X"" : 5, | |||
""Y"" : 7 | |||
} | |||
} | |||
] | |||
}"; | |||
var ser = new JavaScriptSerializer(); | |||
ser.Deserialize<Foo>(json); | |||
} | |||
} | |||
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; } | |||
} | |||
public class NameTypePair | |||
{ | |||
public string ObjectName { get; set; } | |||
public string ObjectType { 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; } | |||
} | |||
} | |||
</pre> | |||
=NewtonSoft JSON= | |||
* http://json.codeplex.com/ | |||
* http://james.newtonking.com/ | |||
[[Category:CSharp]] | [[Category:CSharp]] | ||
[[Category:JSON]] | [[Category:JSON]] |
Latest revision as of 09:39, 5 May 2014
Links
See - http://stackoverflow.com/questions/1474377/json-library-for-c-sharp
using System.Collections.Generic; using System.Web.Script.Serialization; namespace GoogleTranslator.GoogleJSON { public class FooTest { public void Test() { const string json = @"{ ""DisplayFieldName"" : ""ObjectName"", ""FieldAliases"" : { ""ObjectName"" : ""ObjectName"", ""ObjectType"" : ""ObjectType"" }, ""PositionType"" : ""Point"", ""Reference"" : { ""Id"" : 1111 }, ""Objects"" : [ { ""Attributes"" : { ""ObjectName"" : ""test name"", ""ObjectType"" : ""test type"" }, ""Position"" : { ""X"" : 5, ""Y"" : 7 } } ] }"; var ser = new JavaScriptSerializer(); ser.Deserialize<Foo>(json); } } 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; } } public class NameTypePair { public string ObjectName { get; set; } public string ObjectType { 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; } } }