0

I'm working in C# code and the JSON service result I get looks like this:

[
{"value":"{\"code\":\"MO\",\"description\":\"Monday\",\"isSet\":false}","nr":1}
,{"value":"{\"code\":\"TU\",\"description\":\"Tuesday\",\"isSet\":true}","nr":2}
]

I want to work with a list of deserialized value objects from this array. But my impression is that my code is a bit cumbersome.

JArray j = JArray.Parse(task.Result); 
List<Booking> b = j.Select(x => JObject.Parse(x["value"].ToString()).ToObject<Booking>()).ToList();

Is selecting strings and parsing them as objects which then get casted really the way to do this? Or can this be done more efficient?

1 Answer 1

1

Use the following classes:

public class Value
{
    public string Code { get; set; }
    public string Description { get; set; }
    public bool IsSet { get; set; }
}

public class RootObject
{
    [JsonProperty("Booking")]
    public Value Value { get; set; }
    public int Nr { get; set; }
}

and then deserialize your JSON string like this:

var test = JsonConvert.DeserializeObject<List<RootObject>>(json);

The JSON string needs to be unescaped:

[
{"value":{"code":"MO","description":"Monday","isSet":false},"nr":1},
{"value":{"code":"TU","description":"Tuesday","isSet":true},"nr":2}
]

Also the issue with the value is the double quotes. Remove them, and the deserialization works fine.

Update

If you do not want to manipulate the JSON string you can deserialize it twice. For this, I changed the classes like follows:

public class Value
{
    public string Code { get; set; }
    public string Description { get; set; }
    public bool IsSet { get; set; }
}

public class RootObject
{
    public string Value { get; set; }
    public int Nr { get; set; }
}

public class ResultRootObject
{
    public Value Value { get; set; }
    public int Nr { get; set; }
}

Then I could deserialize it to ResultRootObject:

var rootObjects = JsonConvert.DeserializeObject<List<RootObject>>(badJson, new JsonSerializerSettings());
var result = rootObjects.Select(item => new ResultRootObject
{
    Value = JsonConvert.DeserializeObject<Value>(item.Value),
    Nr = item.Nr
}).ToList();
Sign up to request clarification or add additional context in comments.

6 Comments

Yes, that was the other path I examined. But there are a couple of issues. A. Deserializing crashes because it seems that the value property doesn't contain a proper object, but a string that looks like an object. B. My Value object is not named Value, but booking. So I was wondering how to get from Value to Booking, without that becoming to cumbersome as well.
Yes it does. I also found that out by manually manipulating the JSON. Taking out those additional quotes. But, can this be done automatically as well? With some parameter on the deserializer for example?
@Tys regular expression is the only way I can think of.
Feels a bit weird to start doing such things. What could be the reason for the service developers to produce a result like this?
@Tys it does but interestingly, I tried this: freeformatter.com/json-formatter.html and it could extract the inner object.
|

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.