Question

I'm looking for a C# (.NET) library that parses stringified JSON to an Object.

The problem with Newtonsoft.Json.JsonConvert, to me, is that you always need a concrete type for deserialization:

 SomePredefinedType bla = JsonConvert.DeserializeObject<SomePredefinedType>(stringifiedJson);

So I end with many classes that I don't use anymore.
class SomePredefinedType is used once, in that line, and never more.

Of course the navigation is then nicer, you can call properties just like:

string name = bla.Name;
string age = bla.Age;

In Java, I've used org.json, in which the deserialization goes likes this:

JSONObject jsonObject = new JSONObject(stringifiedJson)

The navigation requires that you know what the JSON structure is. But this is always a fact (otherwise how could I create the SomePredefinedType class?)

jsonObject.getString("Name");
jsonObject.getString("Age");

The question is:
Is there a org.json for C#? I don't like Newtonsoft.Json.
I like this way of parsing anonymously.






Edit: Ok, I tend to use simplified examples in my posts, because I don't like when people post a bunch of code. I like to talk on the basis, thats why I post simple code.

Here is what I can't achieve in a simple manner, as with org.json I would:

        // Here not using any object, just as answers below stated I could:
        dynamic jsonProviders = JsonConvert.DeserializeObject(stringifiedJson);

        foreach (dynamic jsonProvider in jsonProviders)
        {
            // Fetch provider
            SMSProvider provider = db.SMSProviders.SingleOrDefault(p => p.SMSProviderId == jsonProvider.SMSProviderId);     // What the hell do I use here? thats an error
            CheckIfExists(provider);

            provider.Order = jsonProvider.Order;
        }
Was it helpful?

Solution 2

Update based on your recent code, I'd say you're almost done. You could simply do this:

int id = jsonProvider.SMSProviderId;
SMSProvider provider = db.SMSProviders.SingleOrDefault(p => p.SMSProviderId == id);

Capturing the id this way should let EF (or whatever that is) understand exactly how to work with the value that came from the JSON.


Json.NET (JsonConvert) is a great library. Here are some options for doing what you want without having more classes than necessary:

// stringifiedJson is {"Name":"George","Age":25}
{
    dynamic deserializedDynamic = JsonConvert.DeserializeObject(stringifiedJson);
    string name = deserializedDynamic.Name;
    int age = deserializedDynamic.Age;
}
{
    var deserializedAnon = JsonConvert.DeserializeAnonymousType(stringifiedJson,
                             new { Name = default(string), Age = default(int) });
    string name = deserializedAnon.Name;
    int age = deserializedAnon.Age;
}
{
    var deserializedDict =
  JsonConvert.DeserializeObject<Dictionary<string, object>>(stringifiedJson);
    string name = (string)deserializedDict["Name"];
    // Age is a long, two casts required
    int age = (int)(long)deserializedDict["Age"];
}
{
    var deserializedDictDyn =
  JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(stringifiedJson);
    string name = deserializedDictDyn["Name"];
    // Age is a long, one cast required with dynamic
    int age = (int)deserializedDictDyn["Age"];
}
{
    var deserializedJObject = JObject.Parse(stringifiedJson);
    string name = (string)deserializedJObject.GetValue("Name");
    int age = (int)deserializedJObject.GetValue("Age");
}

OTHER TIPS

The problem with Newtonsoft.Json.JsonConvert, to me, is that you always need a concrete type for deserialization:

Not necessarily:

dynamic result = JsonConvert.DeserializeObject("{\"Name\":\"John\",\"Age\":12}");
string name = result.Name;
int age = result.Age;

Alternatively:

var result = JObject.Parse("{\"Name\":\"John\",\"Age\":12}");
string name = result["Name"].Value<string>();
int age = result["Age"].Value<int>();

And if you don't want to use third party libraries you could achieve similar results with the built into .NET JavaScriptSerializer class:

var serializer = new JavaScriptSerializer();
var result = (IDictionary<string, object>)serializer.DeserializeObject("{\"Name\":\"John\", \"Age\":12}");

string name = (string)result["Name"];
int age = (int)result["Age"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top