0

I have the following string of Json records:

{
   "records":[
      {
         "PK":"1_1_8",
         "ID":"8",
         "DeviceID":"1",
         "RootID":"1",
         "CustName":"test1",
         "CustSurname":"test2",
         "Address":"Nisou 1",
         "City":"",
         "ZipCode":"",
         "PhoneNumber":"45646",
         "HomePhoneNumber":"",
         "Email":"",
         "Notes":"",
         "Owner":"1",
         "LanguageID":"1",
         "LanguagePK":"",
         "DeletedFlag":"false",
         "created":"2017-10-25 10:15:00",
         "modified":"2017-10-25 09:35:43"
      },
      {
         "PK":"1_1_33",
         "ID":"33",
         "DeviceID":"1",
         "RootID":"1",
         "CustName":"",
         "CustSurname":"",
         "Address":"",
         "City":"",
         "ZipCode":"",
         "PhoneNumber":"",
         "HomePhoneNumber":"",
         "Email":"",
         "Notes":"",
         "Owner":null,
         "LanguageID":"0",
         "LanguagePK":"",
         "DeletedFlag":"true",
         "created":"2017-10-25 10:13:54",
         "modified":"2017-10-25 10:13:54"
      },
      {
         "PK":"1_1_16",
         "ID":"16",
         "DeviceID":"1",
         "RootID":"1",
         "CustName":"Theodosis",
         "CustSurname":"",
         "Address":"Dali",
         "City":"Nicosia",
         "ZipCode":"2540",
         "PhoneNumber":"45645",
         "HomePhoneNumber":"99123456",
         "Email":"theodosis@gmail.com",
         "Notes":"",
         "Owner":"",
         "LanguageID":"1",
         "LanguagePK":"",
         "DeletedFlag":"false",
         "created":"2017-10-25 09:36:22",
         "modified":"2017-10-25 09:36:22"
      }
   ]
}

I am using Xamarin PCL in C# trying to parse this string into a list of objects.

I have a Customer class:

public class Customer
{
    [PrimaryKey]
    public string PK { get; set; }
    public int DeviceID { get; set; }
    public int ID { get; set; }

    public string RootID{ get; set; }

    public string CustName { get; set; }
    public string CustSurname { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string ZipCode { get; set; }
    public string PhoneNumber { get; set; }
    public string HomePhoneNumber { get; set; }
    public string Email { get; set; }
    public string Notes { get; set; }
    public bool Owner { get; set; }

    public int LanguageID { get; set; }
    public string LanguagePK { get; set; }

    public bool DeletedFlag { get; set; }
    public DateTime created { get; set; }
    public DateTime modified { get; set; }
}

I also tried out having a container class with a list of Customer objects.

public class DataContainer
{
    public List<Customer> customers { get; set; }
}

I have seen quite a few of examples online on how to parse this into a list or any workable type but nothing seems to be working for me.

I have tried the following (JsonResults holds the string of Json records):

var observation = JsonConvert.DeserializeObject<DataContainer>(JsonResults);

From other posts, I am not able to access JavaScriptSerializer class from my code, perhaps because of the Xamarin PCL Framework I am using.

Any ideas would be very welcome, as I said I do not mind the format I parse the string into, as long as it's workable.

Thank you.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
user292565
  • 31
  • 1
  • 6
  • 1
    Yous Json starts with an object containing a property `records`. Have you tried renaming (or annotating) `customers` to `records`? – Manfred Radlwimmer Oct 25 '17 at 09:02
  • `JavaScriptSerializer` is obsolete, Everyone uses Json.NET. If you have a problem, it doesn't mean that Json.NET is broken, it means the code is wrong. Post the *error message*. Most likely it complains that your string doesn't match your class (it doesn't). Json has a `records` property whil your class has a `customers` property – Panagiotis Kanavos Oct 25 '17 at 09:02
  • 1
    if you observe that your json is having key as "records" but in your DataContainer you have property as customers – Venkatesh Konatham Oct 25 '17 at 09:02
  • Also you need a JsonConverter for `Owner` or change it to a `int?`. – Manfred Radlwimmer Oct 25 '17 at 09:07
  • 1
    Working example: https://dotnetfiddle.net/Widget/d8MJWT – Manfred Radlwimmer Oct 25 '17 at 09:08

3 Answers3

0

The JSON string you provided is a JSON object, which contains a single property called records. records property is a List<Customer>. You can not deserialize the given string directly into DataContainer class that you provided because the property names do not match.

In the Class that your provided it is called customers

public class DataContainer {
    public List<Customer> customers { get; set; } //records
}

Or please have a look at the attribute for a bit of advanced mapping

[JsonProperty]

JSON you provided is of the form:

{"records":[{Customer},{Customer},{Customer}]}

But Owner property is "1", null or "". Therefore I would suggest redefining Owner as int? (nullable)

Hasan Emrah Süngü
  • 3,488
  • 1
  • 15
  • 33
0

You would have to make the following changes to your code to make this work.

First and most importantly, you don't have a property customers, you have records, so either rename it

public class DataContainer {
    public List<Customer> records { get; set; }
}

or add a JsonProperty attribute

[JsonProperty(PropertyName = "records")]

Secondly, your Owner is a bool in C# and a nullable int (int?) in Json. So either change it in your C# class

public int?  Owner { get; set; }    

or write a converter to do that (e.g. like here)

[JsonConverter(typeof(NullableIntToBooleanConverter))]
public bool Owner { get; set; }

Here is a working .NetFiddle

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
-2

Your string shows one object with a property named records that contains a list of other objects. Your code is trying to deserialize this into an object that doesn't have such a property.

Furthermore, the string contains objects with a property Owner that may be missing or have a numeric value. It's definitely not a bool.

You'll have to change Owner to :

public int?  Owner { get; set; }

To deserialize the string, you need an object with a records property:

public class DataContainer
{
    public Customer[] records { get; set; }
}

var data=JsonConvert.DeserializeObject<DataContainer>(json);
Debug.Assert(data.records.Length == 3);
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236