Home > Software design >  How to Convert Json list String to List in c#?
How to Convert Json list String to List in c#?

Time:07-13

I need to convert a json list string to a C# list

My Model:

public class AddressModel
{
    public string StateCode { get; set; }
    public string CityCode { get; set; }
    public string PinCode { get; set; }
    public string Place { get; set; }
    public string Street { get; set; }
    public string DoorNo { get; set; }
    public string State { get; set; }
    public string City { get; set; }
    public string AddresDetails{ get; set; }
    
}

Json:

"[\n  {\n    \"input\": {\n      \"statecode\": 1,\n      \"citycode\": 12,\n      \"pincode\": \"123\",\n       \"addresdetails\": \"\"\n    },\n    \"output\": [\n      \n    ]\n  },\n  {\n    \"input\": {\n      \"statecode\": 2,\n      \"citycode\": 45,\n      \"pincode\": \"765\",\n        \"addresdetails\": \"\"\n    },\n    \"output\": [\n      \n    ]\n  }\n]";

I tried with DeserializeObject but it's not working

var model = JsonConvert.DeserializeObject(json);

I would like to convert this into a list . could someone help me with this?

CodePudding user response:

Your json is a little bit more than just an array of your models. They're inputs of the objects in the array. You have to do do a little bit more to get it in the form to be deserialized, either by adding more models, or manipulate the json objects.

var obj = JsonConvert.DeserializeObject<JArray>(json);
var addresses = obj.Select(x => x["input"].ToObject<AddressModel>()).ToList();

CodePudding user response:

you have to fix your classes

 List<Data>  data = JsonConvert.DeserializeObject<List<Data>>(json);

public partial class Data
{
    [JsonProperty("input")]
    public AddressModel Input { get; set; }

    [JsonProperty("output")]
    public List<Output> Output { get; set; }
}

public partial class AddressModel
{
    [JsonProperty("statecode")]
    public long Statecode { get; set; }

    [JsonProperty("citycode")]
    public long Citycode { get; set; }

    [JsonProperty("pincode")]
    
    public long Pincode { get; set; }

    [JsonProperty("addresdetails")]
    public string Addresdetails { get; set; }
}

public partial class Output
{
}

if you need just a list of input

 List<AddressModel> inputs = data.Select(d =>d.Input ).ToList();

 // or just using AddressModel

List<AddressModel> inputs = JArray.Parse(json).Select(d =>d["input"].ToObject<AddressModel>() ).ToList();

CodePudding user response:

Here is a sample that works for me

//Deserialize the JSON string with the model
List<Root> myList = JsonConvert.DeserializeObject<List<Root>>(strJSON);

foreach(Root rootObj in myList) {
    //access properties of the deserialized object
    Console.WriteLine(rootObj.Input.statecode);
}

Update: For your JSON, your model should be something like that;

public class Input
    {
        public int statecode { get; set; }
        public int citycode { get; set; }
        public string pincode { get; set; }
        public string addresdetails { get; set; }
    }

    public class Root
    {
        public Input input { get; set; }
        public List<object> output { get; set; }
    }
  • Related