0

I have problem with deserialize json object. I read many threads on stackoverflow but I didn't find solution. I use .net 2.0 and Newtonsoft library.

Below is json string:

{
    "data": {
        "custom_fields": [{
            "field": "segmentid",
            "value": "B"
        },
        {
            "field": "subsegmentid",
            "value": "TM3"
        },
        {
            "field": "contactpersonid",
            "value": "000187_003"
        },
        {
            "field": "firstname",
            "value": "ZBIGNIEW"
        },
        {
            "field": "agreetment",
            "value": "1"
        },
        {
            "field": "contactname",
            "value": "ZBIGNIEW TESTOWY"
        },
        {
            "field": "decisionmaking",
            "value": "0"
        },
        {
            "field": "lastpurchase",
            "value": ""
        },
        {
            "field": "agethresholds",
            "value": "0"
        },
        {
            "field": "tendercust",
            "value": ""
        }],
        "email": "[email protected]",
        "state": "1"
    },
    "status": "OK"
}

I developed two classes but I still get below error.

Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.List`1[FMIntegration.DataFM]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\r\nPath 'data.email'

var SubscriberGet = JsonConvert.DeserializeObject<SubscriberGet>(json); 

public class SubscriberGet
{
    [JsonProperty("status")]
    public string Status { get; set; }

    [JsonProperty("data")]
    public List<DataFM> Data { get; set; }
}

public class DataFM
{
    [JsonProperty("email")]
    public string Email { get; set; }

    [JsonProperty("custom_fields")]
    public List<String> custom_fields { get; set; }

    [JsonProperty("state")]
    public string State { get; set; }
}
2
  • Custom fields is not a list of strings. Commented Feb 25, 2014 at 15:04
  • 2
    See what this site (json2csharp.com) generates for you. Commented Feb 25, 2014 at 15:06

2 Answers 2

4

Custom fields is not a list of strings but a list of "field and value" pair.

Try this:

public class SubscriberGet
{
    [JsonProperty("status")]
    public string Status { get; set; }

    [JsonProperty("data")]
    public List<DataFM> Data { get; set; }
}

public class DataFM
{
    [JsonProperty("email")]
    public string Email { get; set; }

    [JsonProperty("custom_fields")]
    public List<CustomField> custom_fields { get; set; }

    [JsonProperty("state")]
    public string State { get; set; }
}
public class CustomField
{
    [JsonProperty("field")]
    public string field { get; set; }
    [JsonProperty("value")]
    public string value { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

0

This is correct objects to handle my json string (thanks L.B. for link)

public class CustomField
{
    public string field { get; set; }
    public string value { get; set; }
}

public class Data
{
    public List<CustomField> custom_fields { get; set; }
    public string email { get; set; }
    public string state { get; set; }
}

public class RootObject
{
    public Data data { get; set; }
    public string status { get; set; }
}

2 Comments

you should award user1477388 the answer as he specified just that. No need to create an identical answer.
jgauffin: if you read answers one again (carefully) you will see differences. user1477388 codes doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.