1

I want to parse a piece of JSON with Newtonsoft Json.NET

JSON:

[{
"type": "switchStatus",
"Data" :[
            { 
            "ID" : "1",
            "value" : "2.5"
        },
        { 
            "ID" : "2",
            "value" : "4.2"
        }
    ],
"Datetime": "2014-12-01",
"customerID": "50"
}]

Classes:

public class Account
{
    [JsonProperty("type")]
    public string Type { get; set; }
    public List<Data> Data { get; set; }
    [JsonProperty("Datetime")]
    public string DateTime { get; set; }
    [JsonProperty("customerID")]
    public string CustomerId { get; set; }
}//Account

public class Data
{
    [JsonProperty("ID")]
    public string Id { get; set; }
    [JsonProperty("value")]
    public string Value { get; set; }
}

Parsing:

Account account = JsonConvert.DeserializeObject<Account>(message);

Error :

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'JSonParser.Account' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

Path '', line 1, position 1.

2 Answers 2

5

Your problem is that your JSON doesn't match your declared class. Specifically, your Data property isn't a List<string>, its a complex object.

Your class should look like this:

public class Data
{
    [JsonProperty("ID")]
    public int Id { get; set; }
    [JsonProperty("value")]
    public double Value { get; set; }
}

public class Account
{
    [JsonProperty("type")]
    public string Type { get; set; }
    public List<Data> Data { get; set; }
    [JsonProperty("Datetime")]
    public string DateTime { get; set; }
    [JsonProperty("customerID")]
    public int CustomerId { get; set; }
}

Edit:

As you've edited your JSON, it's clear now that you need a List<Account>, and not a single one. When you deserialize, you'll need:

List<Account> accounts = JsonConvert.DeserializeObject<List<Account>>(message);
Sign up to request clarification or add additional context in comments.

6 Comments

My reply is identical to @YuvalItzchakov while his is more accurate.
@SajjadZare I've tested this code and it works. Are you sure you're running the correct code?
@SajjadZare You only copy and pasted my code. I don't follow you. I've run this code and it works fine. Are you still getting the same error?
posted JSON is an array of Account objects, so problem is also in deserialize call.
@SajjadZare My answer has a "Edit" in bold. Look at it.
|
1

You cannot Deserialize the data Data attribute like that. Since the list isnt a string to start with.

Try:

public class Data
{
    public int ID { get; set; }
    public float value { get; set; }
}//Data

public class Account
{
    public string type { get; set; }
    public List<Data> Data { get; set; }
    public string Datetime { get; set; }
    public string customerID { get; set; }
}//Account

1 Comment

If you declare value as an int, you're truncating any decimal point.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.