1

I have JSON:

{"customer":[{"phone":"9868133331"},{"phone":"9971714514"}],"message":[{"type":"reminder"},{"type":"reminder"}]}

Which is formatted as:

{
  "customer": [
    {
      "phone": "9868133331"
    },
    {
      "phone": "9971714514"
    }
  ],
  "message": [
    {
      "type": "reminder"
    },
    {
      "type": "reminder"
    }
  ]
}

I am trying to map JSON in a nested class:

public class AllData
{
    public class Message
    {
        public String Type;
    }

    public class Customer
    {
        public String Phone;
    }

    public List<Message> Messages = new List<Message>();
    public List<Customer> Customers = new List<Customer>();
}

with code:

AllData Data = new AllData();
Data = Newtonsoft.Json.JsonConvert.DeserializeObject<AllData>(JSON);

But it gives me empty List of Data.Customers and Data.Messages. The code does not populate the data from JSON to my objects of type List. I mean Data.Customers.Count and Data.Messages.Count is equal to 0

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
Computer User
  • 2,839
  • 4
  • 47
  • 69

4 Answers4

3

You can use VS menu Edit -> Paste special -> Paste JSON as classes and see how your JSON should look like. Actually, it looks like

public class AllData
{
    public Customer[] customer { get; set; }
    public Message[] message { get; set; }
}

public class Customer
{
    public string phone { get; set; }
}

public class Message
{
    public string type { get; set; }
}

You should update property names or decorate them with JsonProperty attribute. You also should specify getter and setter for collection properties

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
1
     var res = Data = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(JSON);


    public class Customer
    {
        public string phone { get; set; }
    }



    public class Message
    {
        public string type { get; set; }
    }

    public class RootObject
    {
        public List<Customer> customer { get; set; }
        public List<Message> message { get; set; }
    }
Or Yaacov
  • 3,597
  • 5
  • 25
  • 49
1

The JSON converter will look for a property called customer, and a property called message. But neither one of those exist in your AllData class.

Observe, if you have data like

var ad = new AllData();
ad.Messages.Add(new AllData.Message() { Type = "reminder" });
ad.Messages.Add(new AllData.Message() { Type = "reminder" });
ad.Customers.Add(new AllData.Customer() { Phone = "9868133331" });
ad.Customers.Add(new AllData.Customer() { Phone = "9971714514" });

then serializing

Newtonsoft.Json.JsonConvert.SerializeObject(ad)

will give the string

"{\"Messages\":[{\"Type\":\"reminder\"},{\"Type\":\"reminder\"}],\"Customers\":[{\"Phone\":\"9868133331\"},{\"Phone\":\"9971714514\"}]}"  

You can re-serialize this back into an AllData object:

AllData ad2 = Newtonsoft.Json.JsonConvert.DeserializeObject<AllData>("{\"Messages\":[{\"Type\":\"reminder\"},{\"Type\":\"reminder\"}],\"Customers\":[{\"Phone\":\"9868133331\"},{\"Phone\":\"9971714514\"}]}");

Now, if you want, you can change your class definition to serialize the JSON message object into the class Messages property using the JsonProperty attribute:

using Newtonsoft.Json;
public class AllData
{
    public class Message
    {
        public String Type;
    }

    public class Customer
    {
        public String Phone;
    }

    [JsonProperty(PropertyName = "message")]
    public List<Message> Messages = new List<Message>();

    [JsonProperty(PropertyName = "customer")]
    public List<Customer> Customers = new List<Customer>();
}

You can then deserialize your original string as given:

AllData ad3 = Newtonsoft.Json.JsonConvert.DeserializeObject<AllData>("{\"customer\":[{\"phone\":\"9868133331\"},{\"phone\":\"9971714514\"}],\"message\":[{\"type\":\"reminder\"},{\"type\":\"reminder\"}]}");
BurnsBA
  • 4,347
  • 27
  • 39
0

i use JsonConvert for deserialize a JSON to this object structure:

public class ResPLP
{
    public string status { get; set; }
    public ResPLPData data { get; set; }
}
public class ResPLPData
{
    public string message { get; set; }
    public string codigo { get; set; }
    public int id { get; set; }
    public string qr { get; set; }
}

and the json structure:

{
"status":"success",
"data": 
{"message":"ok","codigo":"64581D","id":0136,"qr":"4a1db513cde17d4c35cb1d4.png"}
}

And the deserialize object: cleaning manually the string in the content of the result.

var resp = response.Content.ReadAsStringAsync().Result;
resp = resp.Trim("\"".ToCharArray());
resp = resp.Replace("\\", "");
ResPLP resplp = JsonConvert.DeserializeObject<ResPLP>(resp);
codres = resplp.data;