E
E
ellz2019-02-08 19:12:31
JSON
ellz, 2019-02-08 19:12:31

How to convert json to multidimensional array?

There is this JSON:

{
  "cities": {
    "CountryName": [{
        "name": "Sentra"
      },
      {
        "name": "Sentra"
      },
      {
        "name": "Sentra"
      }
    ],
    "CountryName2": [{
        "name": "Sentra"
      },
      {
        "name": "Sentra"
      }
    ]
  }
}

How can I convert it to a multidimensional array of type string[,] cities = new string[,] { };?
I tried through this site , but it gives something wrong:
public class CountryName
{
    public string name { get; set; }
}

public class CountryName2
{
    public string name { get; set; }
}

public class Cities
{
    public List<CountryName> CountryName { get; set; }
    public List<CountryName2> CountryName2 { get; set; }
}

public class RootObject
{
    public Cities cities { get; set; }
}

That is, you need to pre-register each country and city in the class. Maybe with JSON, something is wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Gaydak, 2019-02-08
@ellz

how to convert?? handles ..
that's right, the site gives you ..
two countries in each list of cities ....
you don't have an array inside cities
, maybe you wanted

{
  "cities": [{
    "CountryName": [{
        "name": "Sentra"
      },
      {
        "name": "Sentra"
      },
      {
        "name": "Sentra"
      }
    ],
    "CountryName2": [{
        "name": "Sentra"
      },
      {
        "name": "Sentra"
      }
    ]
  }
]
}

and then you will generate a list of lists.
can you justify why you need a multidimensional array .. and why the list of lists does not suit you ??

D
d-stream, 2019-02-08
@d-stream

There is a simple, lazy way: select (preferably large) JSON and edit->paste special -> paste JSON as Classes in the studio
Then we look at the classes, slightly straighten data types, mark data contracts and members and have happiness in the form of successful serializations starting from DataContractJsonSerializer to any fancy out-of-the-box library/framework (like Json.NET)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question