1

I need to be able to access the code and title properties of each shipping service but the object they're contained in is named differently each time and I don't have control of this.

JSON file

1

3 Answers 3

2

You can parse your JSON object direct to model using Newtonsoft.Json nuget package.

var objData = JsonConvert.DeserializeObject<MyData>(jsonString);

You can get your model class from json data from http://json2csharp.com/

You can convert using dynamic object as well

var objData = JsonConvert.DeserializeObject<dynamic>(jsonString); 

Or without specifying model calss

var objData = JsonConvert.DeserializeObject(jsonString);
Sign up to request clarification or add additional context in comments.

3 Comments

I'm already using JsonConvert for deserializing other json files but cannot in this example as I don't know what the class names would be. For example, the names "Flat" "parcelforce_48" "free" are something I don't have control of. They could be named anything. So I'm unable to create C# classes that represent their data.
Are you able to generate these classes at runtime?
Not run time, but you must know your entities else how can you fetch data from. If you go with dynamic then also you need to know the name of properties from which you want to access the value.
1

The code below use with json.net.

///Custom converter to parse the container.
public class ItemConverter : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var jobj = JObject.Load(reader);

        var item = jobj.First.First.ToObject<Item>();
        var container = new ItemContainer
        {
            Name = jobj.First.Path,
            Data = item
        };
        return container;
    }

    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof (Item);
    }
}

[JsonConverter(typeof(ItemConverter))]
public class ItemContainer
{
    public string Name { get; set; }

    //Your object is here
    public Item Data { get; set; }
}

public class Item
{
    public string code { get; set; }
    //Other properties
}

public class RootObj
{
    public ItemContainer[] ShippingMethods { get; set; }
}

Deserialize json:

JsonConvert.DeserializeObject<RootObj>("Your json string");

3 Comments

ShippingMethods is always null in this example. I have a feeling the ReadJson method is not being called and causing the NULL shipping methods container.
Are you sure? I success to Deserialize your json sample to object.
I made the mistake of making the methods and classes internal. It works once they're made public. Thanks very much!
1

you can use Json.net to deserialize your json into a dynamicvariable to fix the (unknown properties) issue, and access your properties by name assuming you know them.

example:

dynamic parsedObject = JsonConvert.DeserializeObject("{\"id\":\"123\"}");
parsedObject.id // it should read 123

4 Comments

I don't think I'm following you. In the example I've given, how would I access the object Flat's title property.
parsedObject.ShippingMethods[0].title
Doesnt appear to work, I'm getting NULL when trying to access the property. dynamic parsedObject = JsonConvert.DeserializeObject(response.Result); var test = parsedObject.ShippingMethods[0].title;
dynamic parsedObject = JsonConvert.DeserializeObject(response.Result); var test = parsedObject.ShippingMethods[0].flat; var something = test.title; This example works but it relies on me knowing "Flat".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.