0

Hi I have a little problem with deseralizing JSON.

It's json which i get from API

"{\"status\":\"ok\",\"categories\":
[{\"id\":1,\"name\":\"Name\",\"number\":0,\"clientsCount\":32,\"fields\":
[\"name\",\"surname\",\"tel\",\"post\",\"country\"],\"subcategories\":[{\"id\":2,\"name\":\"Got\",\"number\":1},{\"id\":13,\"name\":\"Hipoteka\",\"number\":2},
{\"id\":14,\"name\":\"Samochodowych\",\"number\":4}]},
{\"id\":2,\"name\":\"Name\",\"number\":1,\"clientsCount\":12,\"fields\":
[\"name\",\"nazwisko\",\"tel\",\"car\",\"car_model\"],\"subcategories\":[]}]}"

and this is how it looks like in PHP

array(
                'id' => 1,
                'name' => 'Name',
                'number' => 0,
                'clientsCount' => 32,
                'fields' => array(
                    'name',
                    'surname',
                    'tel',
                    'post',
                    'country'
                ),
                'subcategories' => array(
                    array(
                        'id' => 2,
                        'name' => 'Gotówkowy',
                        'number' => 1
                    ),
                    array(
                        'id' => 13,
                        'name' => 'Hipoteka',
                        'number' => 2
                    ),
                    array(
                        'id' => 14,
                        'name' => 'Samochodowych',
                        'number' => 4
                    ),
                ),
            ),
            array(
                'id' => 2,
                'name' => 'Name',
                'number' => 1,
                'clientsCount' => 12,
                'fields' => array(
                    'name',
                    'surname',
                    'tel',
                    'car',
                    'car_model'
                ),
                'subcategories' => array(),
            ),

I don't have any idea how to convert it. I dont need from this JSON fields subcategories but others are very important for me.

I have prepared some class but I do not know How to load to this class array of 'fields'

class Kategorie
{
   public int id { get; set; }
   public string name { get; set; }
   public int number { get; set; }
   public int clientsCount { get; set; }
}
Liath
  • 9,913
  • 9
  • 51
  • 81
Icet
  • 678
  • 2
  • 13
  • 31
  • 3
    The above array looks more like a PHP array. – haim770 Jan 02 '15 at 07:57
  • What is posted as sample of "JSON" is not JSON at all. Please check if you've posted correct example OR update post to not mention JSON. – Alexei Levenkov Jan 02 '15 at 08:01
  • Sorry I post a php code I edited my question and add JSON string which i get from API – Icet Jan 02 '15 at 08:26
  • Do you really have a \ before every "? Can you post the raw json? – Frank Jan 02 '15 at 08:29
  • Yes I have. I do not understand how I can raw json? I receving this in that way: using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { result = streamReader.ReadToEnd(); } and the result is it this what I post – Icet Jan 02 '15 at 08:32
  • 1
    @Icet Use NewtonSoft JSON library and you can learn a sample [here](http://stackoverflow.com/questions/17038810/newtonsoft-json-deserialize) to deserialize json to C# object. – Siva Gopal Jan 02 '15 at 08:33
  • @Icet Also before deserializing replace the string escape chars: \" with a single quote: ' and then deserialize. – Siva Gopal Jan 02 '15 at 08:36
  • @SivaGopal thanks for advices! I will use them for sure. – Icet Jan 02 '15 at 08:43
  • @Frank the OP most likely copied the string out of the VS intellisense and that always escapes " chars like that – zaitsman Jan 02 '15 at 08:47

1 Answers1

4

Here is a sample based on the Json you posted:

//Created MyObject to resemble the JSON object graph for easy deserialization
class MyObject
{
     public string Status { get; set; }
     public Category[] Categories { get; set; }
     //Similar to categories, You can create properties for fields, subcategories etc., as needed.
}
class Category
{
     public int id { get; set; }
     public string name { get; set; }
     public int number { get; set; }
     public int clientsCount { get; set; }

}

Deserialization Code: (Using Newtonsoft.Json)

var myObject = JsonConvert.DeserializeObject<MyObject>(jsonString);

Hope this help.

Siva Gopal
  • 3,474
  • 1
  • 25
  • 22