0

I have JSON string: // the coverage value is an array.

  string jsonData = @"{
""plans"":
[
{
""plan_code"":""UL500"",
    ""plan_name"":""Unlimited 500M"",
    ""days"":1,
    ""limit"":500,
    ""coverage"":
[
{
""country"":""SE"",
},
{
""country"":""BZ""
}
]
},
{
""plan_code"":""UL1GB"",
    ""plan_name"":""Unlimited 1GB"",
    ""days"":1,
    ""limit"":1024,
    ""coverage"":
[
{
""country"":""SG"",
},
{
""country"":""JP""
}
]
}
]
}
";

and i'm parse by JsonConvert.DeserializeObject as sample code below:

 try
    {
        var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
        HttpResponseMessage response = await client.PostAsync("", content);
        var result = await response.Content.ReadAsStringAsync();
        var tempRecords = JsonConvert.DeserializeObject<List<plan>>(result);
    }

and then i'm get an error message: "Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.List`1[DAL.plan]' 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 'plans', line 1, position 9."

please show me a right way for this issue.

1

4 Answers 4

1

Here is the working code with the same JSON, from my console app:

public class Program
{
    static void Main(string[] args)
    {
        string jsonData = "{\"plans\":[{\"plan_code\":\"UL500\",\"plan_name\":\"Unlimited 500M\",\"days\":1,\"limit\":500,\"coverage\":[{\"country\":\"SE\"},{\"country\":\"BZ\"}]},{\"plan_code\":\"UL1GB\",\"plan_name\":\"Unlimited 1GB\",\"days\":1,\"limit\":1024,\"coverage\":[{\"country\":\"SG\"},{\"country\":\"JP\"}]}]}";

        var tempRecords = JsonConvert.DeserializeObject<RootObject>(jsonData);

    }
}

public class RootObject
{
    public List<plan> plans { get; set; }
}

public class plan
{
    public string plan_code { get; set; }
    public string plan_name { get; set; }
    public int days { get; set; }
    public int limit { get; set; }
    public IList<Country> coverage { get; set; }
}

public class Country
{
    public string country { get; set; }
}

I have created C# classes that suits your JSON structure. As a note, I have modified the JSON you provided, as it seems to be not in the correct format.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! string jsonData in my case in fact is dynamic data, the sample code you give me work well
@MaiTrọngThànhSEOMaster Please mark as answer if this was helpful to solve your issue.
0

The whole string represents an object with a property called "plans" that refers to your array. The string itself is not the array, so if you're trying to deserialize it as a List<T>, that's not going to work. You need to handle the fact that your desired array is wrapped by a JSON object.

This is a similar problem that I think explains it well.

Comments

0

The entity corresponding to the JSON format does not match. Your format is an object, and the object has an array of attributes, not List

Comments

0

Hello,I have just looked at your question, I found that you provided the json string, not the standard format json string. Here I am giving me a good json string, you can try it out。

{
    "plans": [`enter code here`
        {
            "plan_code": "UL500",
            "plan_name": "Unlimited500M",
            "days": 1,
            "limit": 500,
            "coverage": [
                {
                    "country": "SE"
                },
                {
                    "country": "BZ"
                }
            ]
        },
        {
            "plan_code": "UL1GB",
            "plan_name": "Unlimited1GB",
            "days": 1,
            "limit": 1024,
            "coverage": [
                {
                    "country": "SG"
                },
                {
                    "country": "JP"
                }
            ]
        }
    ]
}

1 Comment

Thanks for your guide!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.