0

I have JSON string results as follows. enter image description here

In this response Sometimes sizeKey and sizeName properties are returned as a string. But sometimes both properties are returns inside an array as follows enter image description here

I am using following code to convert it to object

var assets = jObject["assets"].Children().ToList();
 foreach (var item in assets)
 {
 decorationAssets.Add(item.ToObject<AEDecorationAssets>());
 }

And my AEDecorationAssets class is as follows.

public class AEDecorationAssets
{
 public string Id { get; set; }
 public string Url { get; set; }
 public string[] Colors { get; set; }
 public string FontKey { get; set; }
 public string SizeKey { get; set; }
 public string ViewKey { get; set; }
 public string FontName { get; set; }
 public int Rotation { get; set; }
 public string SizeName { get; set; }
 public string TextValue { get; set; }
 public string EntityType { get; set; }
 public string LocationCode { get; set; }
 public string LocationName { get; set; }
 public string TextEffectKey { get; set; }
 public string TextEffectName { get; set; }
 public string DecorationMethod { get; set; }
 public string NumDecorationColors { get; set; }
}

At the time when "sizeKey" is an array, the above code gives an error. How can I resolve this issue? Is there any JSON property we can use to resolve it?

asked May 16, 2019 at 5:56
5

3 Answers 3

3

One way you can do it is by making your SizeKey type an object (i.e. public object SizeKey { get; set; }), then you can switch/case on item.ToObject<AEDecorationAssets>().SizeKey.GetType() to figure out how to handle it (i.e. if String do this, if JArray do that), etc.

answered May 16, 2019 at 6:16

2 Comments

If(item.ToObject<AEDecorationAssets>().SizeKey.GetType() == typeof(JArray)){((JArray)SizeKey).ToObject<List<CustomObject>>();}
@udaya726 don't forget your null checks!
0

If a JSON type is sometime an array, and sometimes a string, you can't really map it simply to a .NET type, as there is none that supports this behavior.

So first you need a datatype that can store this, like and string[] or List<string>.

It could be that JsonConvert will solve this automatically, but otherwise you'll need to write a custom ContractResolver or JsonConverter. Here you can detect if the source property is a string or array. If it's an array, you can use the default deserialization. If it is a string, you need to convert it to an array with a single value.

answered May 16, 2019 at 6:17

Comments

0

Simply get json result for which you want to create c# object and then you can valid json response from https://jsonlint.com/ and then you can create c# object of any type json response which you want through http://json2csharp.com. And after get c# object of your json response you only need to deserialization of your json response to c# object which you have created. which will return you expected result.

answered May 16, 2019 at 6:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.