-1

I have to parse a Web API in the following format. Please be aware that I cannot change the format of the JSON. It will always come in in this format:

{
    "somethingone": "abc",
    "somethingtwo": "abcde-1234",
    "information": {
        "report": [{
                "a": "1",
                "b": "2",
                "c": "3"
            },
            {
                "a1": "1a",
                "b2": "2a",
                "c3": "3a"
            }, {
                "a1": "1b",
                "b2": "2b",
                "c3": "3b"
            },
        ]
    }
}

When I try to parse it in Newtonsoft, I get the following error message:

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

I have been trying to solve this issue for days, but cannot figure this out.

jazb
  • 5,498
  • 6
  • 37
  • 44
  • It would be awesome if you could provide a [mcve] – jazb Dec 13 '18 at 05:19
  • You can find answere here https://stackoverflow.com/questions/22557559/cannot-deserialize-the-json-array-e-g-1-2-3-into-type-because-type-requ – mk Mughal Dec 13 '18 at 05:19
  • 1
    Possible duplicate of [Cannot deserialize the JSON array (e.g. \[1,2,3\]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctly](https://stackoverflow.com/questions/22557559/cannot-deserialize-the-json-array-e-g-1-2-3-into-type-because-type-requ) – mk Mughal Dec 13 '18 at 05:20
  • Depending on where you get the json string couldn't just append a [ to the front and a ] to the end – Ahh ... It's a programming thi Dec 13 '18 at 05:28

2 Answers2

1

In this problem, possibility you are parsing your json as list of your class like List<ClassName> you should exclude List<> because you have single main object in incoming json

mk Mughal
  • 286
  • 4
  • 16
0

If your items in array of report is not fixed means these items are with count from 1 to N then declaring property for each of item is hard and your class objects structure become tedious.

So you need collect all your items in Dictionary so it can parse your items from number 1 to N.

These class object are suitable to your json.

class RootObj
{
    public string somethingone { get; set; }
    public string somethingtwo { get; set; }
    public Information information { get; set; }
}

class Information
{
    public Dictionary<string, string>[] report { get; set; }
}

And you can deserialize like

RootObj mainObj = JsonConvert.DeserializeObject<RootObj>(json);

Console.WriteLine("somethingone: " + mainObj.somethingone);
Console.WriteLine("somethingtwo: " + mainObj.somethingtwo);

foreach (Dictionary<string, string> report in mainObj.information.report)
{
    foreach (KeyValuePair<string, string> item in report)
    {
         string key = item.Key;
         string value = item.Value;

         Console.WriteLine(key + ": " + value);
    }
}

Console.ReadLine();

Output:

enter image description here

Live Demo

er-sho
  • 9,581
  • 2
  • 13
  • 26
  • I saw in your new question that you used my above answer code. but never reply me whether its solved your problem or not. anyways your new question is extended scenario of your need but if my answer was helpful to you then you may accept my answer by clicking the tick on left side of answer to make it green :) – er-sho Dec 15 '18 at 06:51