0

I want to use the standard .NET library without any extra references. I did a search on Google but I didn't find any classes I can use.

The data model (attributes) is also based on a configuration file, so creating a class for the objects is not a good idea because it only makes my app uncofigurable.

any advice?

Ozkan
  • 3,880
  • 9
  • 47
  • 78

5 Answers5

0

Grab the source code from the JSON.Net library, add it to your solution, and reference it in your projects.

https://github.com/JamesNK/Newtonsoft.Json/tree/master/Src/Newtonsoft.Json

You now have all the features of JSON.Net without any external dependencies.

Then, when you get tired of manually updating it, relax your "only in the framework" rule and add it via Nuget so it's super-easy to manage.

EDIT: There is the two built-in JSON serializers, but they both suck in their own special ways.

JavaScriptSerializer UTC DateTime issues

JavaScriptSerializer, and DataContractJsonSerializer are riddled with bugs. Use json.net instead. Even Microsoft has made this switch in ASP.Net MVC4 and other recent projects.

Community
  • 1
  • 1
0

I am not sure there is an easy way to do this using just the framework classes

The Json.Net library (Which is used by Web API and such) allows you to do this quite easily though. See http://www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm for an example.

Simon Bull
  • 851
  • 6
  • 14
0

The JavaScriptSerializer Class has functions for converting between a JSON object and a string. Maybe something you can take a look at?

Jesper Evertsson
  • 613
  • 9
  • 28
0

Have you tried SimpleJSON from here: http://wiki.unity3d.com/index.php/SimpleJSON ? The single page of source code is displayed at that link. It does an ok job of parsing strings. Obviously it's got nothing on JSON.Net though.

Pricey
  • 308
  • 3
  • 13
  • Nice link! Handy if you want to quickly drop in a single code file. –  Jan 18 '16 at 12:16
0

If you add a reference to System.Web.Extensions to your project you can use the JavaScriptSerializer class to deserialize your string into json:

var json = new JavaScriptSerializer().Deserialize<MyType>(myString);

You can also do this dynamically, so you don't need to define a type:

string myString = @"
{
    ""Hello"": 1,
    ""World"": ""FooBar""
}";

dynamic json = new JavaScriptSerializer().DeserializeObject(myString);
int hello = json["Hello"];
string world = json["World"];
TVOHM
  • 2,740
  • 1
  • 19
  • 29