0

Returns Json format data to me from an api.

However, the type of the "FuelType" property inside the object may be different. For example, 1 time comes as follows:

{
...
fuelType: "gasoline"
...
}}

But then it can happen:

{
...
fuelType: ["gasoline", "any"]
...
}}

If I set the "FuelType" property type on my model to a string, in the second case, Json will give me an error when it arrives, because it can't convert from array to string. No, if I set the type to an array, then, conversely, if a string arrives, it will issue an error because it cannot convert from a string to an array.

In this case, what should I put the "FuelType" property type in my model so that it does not make an error when deserializing?

1
  • If it can return a array...Jut make it an array? Commented Apr 1, 2022 at 13:47

2 Answers 2

1

It all depends on you,what type of value you want to receive?string or List?

I tried with the codes:

public class SomeModel
    {
        public SomeModel()
        {
            fuelType = new List<string>();            
        }
        public int Id { get; set; }
        public string  Name { get; set; }
        
        public List<string> fuelType { get; set; }

        //you could move the codes to someservice
        public List<string> someresult(string a)
        {
            var targetlist = new List<string>();
            targetlist.Add(a);
            return targetlist;
        }
        public List<string> someresult(List<string> a)
        {
            var targetlist = new List<string>();
            targetlist.AddRange(a);
            return targetlist;
        }
    }

[HttpPost]
        public JsonResult SomeAction()
        {
            var somemodel = new SomeModel() { Id = 1, Name = "name" };
            var somevalue = /*"a"*/new List<string>() { "a", "b", "c" };
            var targetvalue = somemodel.someresult(somevalue);
            somemodel.fuelType.AddRange(targetvalue);
            return new JsonResult(somemodel);
        }

Result: enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

try this

var fuelType = JsonConvert.DeserializeObject<MyClass>(json);

public class MyClass
{
    [JsonProperty("fuelType")]
    private JToken _fuelType;

    [JsonIgnore]
    public string[] fuelType
    {
        get {
if (_fuelType==null) return null;
 return _fuelType is JArray ? _fuelType.ToObject<string[]>() : new string[] { (string)_fuelType }; }
        set { _fuelType = JsonConvert.SerializeObject(value); }
    }
}

5 Comments

_fueltype fieldi but comes null. Can you help with that?
The field still remains at null value. I think that's why it's null. ( Code image link : imgur.com/B8YV0XB )
@Elchin It is just a warning. Do you have any bugs when you are deserializing a json?
No. Everything except FuelType is properly deserialized
This code was tested in visual studio and working properly. Can you post your json that can not be deserialized?

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.