0

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?

3 Answers 3

1

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();
Sign up to request clarification or add additional context in comments.

Comments

1

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; }
    }

3 Comments

not for that json...
Where I have to use these two models.can you please explain?
@itsme, regarding your JSON string, you have an array of objects and each object has input and output properties. And, input object has child properties, such as statecode, citycode, etc. So, you need an Input object and the input object is a property of the Root object. These are your models for deserialization, which is JsonConverter expects those strongly typed models for deserializing. I am going to edit my answer using that model for better clarification.
0

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();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.