0

I am trying to deserialize a Json response from an API. The data looks like this.      

 {
      "api": {
        "results": 1,
        "aaa": [
          [
            {
              "bbb": 1,
              "ccc": 85,
              "ddd": "this is  test1",
              "eee": "this is  test2",
              "fff": "orang",
              "ggg": "apple",
              "hhh": "this is test3",
              "iii": {
                "iia": 35,
                "iib": 27,
                "iic": 4
              },
              "jjj": {
                "jja": 18,
                "jjb": 16,
                "jjc": 2
              },
              "kkk": {
                "kka": 17,
                "kkb": 11,
                "kkc": 2,

              },
              "lll": 67,
              "mmm": 85,
              "nnn": "2019-05-04"
            }
          ]
        ]
      }
    }

and, for more information about Json schema for this case is 

 {
      "type": "object",
      "properties": {
        "api": {
          "type": "object",
          "properties": {
            "results": {
              "type": "integer"
            },
            "aaa": {
              "type": "array",
              "items": {
                "type": "array",
                "items": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    }

and i have create a custom class to read the json content http://json2csharp.com/ :

public class Api
{
    public int results { get; set; }
    public List<List<>> aaa { get; set; }
}

public class RootObject
{
    public Api api { get; set; }
}

what is correct class for this json? What should I do to correct this?

3
  • Take a look at this line public List<List<>> aaa { get; set; } That just won't compile, needs to have a type, I'm assuming you could KeyValuePair? Commented Sep 3, 2019 at 9:40
  • Is wired to have a List in a List, with no defined object inside, are you sure that the json is correct? if you delete a pair of [ ], the generated class is really different Commented Sep 3, 2019 at 9:48
  • Json is modified enought to remove the meaning. I will recommend making a minimal reproducible example with correct name. It's hard to know if aa bb cc are diferent property or if it should be deserialised to a dictionnary<string,string>. Commented Sep 3, 2019 at 13:39

2 Answers 2

1

You must create a class for each object. so you must create a class for this section:

"items": {
            "type": "object"
         }

and then use this class in this line:

public List<List<Item>> aaa { get; set; }
Sign up to request clarification or add additional context in comments.

Comments

0

According to the schema you've provided, this should work:

public class Api
{
    public Dictionary<string, object>[][] Aaa { get; set; }
    public long? Results { get; set; }
}

But if you want to fully deserialize the class, you might use something like this:

public class FinalClass
{
    public Api Api { get; set; }
}

public class Api
{
    public long Results { get; set; }

    public Aaa[][] Aaa { get; set; }
}

public class Aaa
{
    public long Bbb { get; set; }

    public long Ccc { get; set; }

    public string Ddd { get; set; }

    public string Eee { get; set; }

    public string Fff { get; set; }

    public string Ggg { get; set; }

    public string Hhh { get; set; }

    public Dictionary<string, long> Iii { get; set; }

    public Dictionary<string, long> Jjj { get; set; }

    public Dictionary<string, long> Kkk { get; set; }

    public long Lll { get; set; }

    public long Mmm { get; set; }

    public DateTimeOffset Nnn { get; set; }
}

Comments

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.