1

I have a problem with JSON.

"{"status":"ok","message":"Dane klienta zostau0142y pobrane pomyu015blnie","clientData":
{"id":22,"imie":"Pppppppppp","nazwisko":"Ppppppppppppp","tel":"111111126","email":"[email protected]","ulica":"Na Przyzbie","nr_budynku":"3","nr_lokalu":"41","kod_pocztowy":"02-813","miejscowosc":"Warszawa","samochod_marka":"opel","samochod_model":"vectra","subcategories":
{"6":200}}}"

and it's my class

public class Client
    {
        public string Status { get; set; }
        public string Message { get; set; }
        public Data clientData { get; set; }
    }

    public class Data
    {
        public Dictionary<string, string> clientData { get; set; }
    }

everything is mostly correct but when I debug my code field clientData is null.

What am I doing wrong?

Thanks for help!

EDIT:

it's how I deserialize object.

var myObject = JsonConvert.DeserializeObject<Client>(get_person);
13
  • Show us the code that is causing you the problem. Commented Jan 7, 2015 at 10:16
  • 2
    subcategories is not of type string. Commented Jan 7, 2015 at 10:17
  • How do you convert this? Where is that code? Commented Jan 7, 2015 at 10:18
  • I have make edit how I convert it. Commented Jan 7, 2015 at 10:19
  • 1
    @MelanciaUK: The outer quotes are highly likely from copying the value from the debugger. I know the OP doesn't really have those quotes as part of the input because if they did then clientData wouldn't be the only problem Commented Jan 7, 2015 at 10:24

3 Answers 3

1

The problem with your current attempt is that you are trying to convert clientData to a Dictionary<string, string>. This is causing an issue because not all of your values are strings, the problematic ones are as follows:

id : int
subcategories : Dictionary<string, int>

If you don't want to explicitly define all of your properties due to them changing without notice, then I would recommend a change to your JSON structure as follows:

{
    "status": "ok",
    "message": "Dane klienta zostau0142y pobrane pomyu015blnie",
    "clientData": {
        "id": 22,
        "properties": {
            "imie": "Pppppppppp",
            "nazwisko": "Ppppppppppppp",
            "tel": "111111126",
            "email": "[email protected]",
            "ulica": "Na Przyzbie",
            "nr_budynku": "3",
            "nr_lokalu": "41",
            "kod_pocztowy": "02-813",
            "miejscowosc": "Warszawa",
            "samochod_marka": "opel",
            "samochod_model": "vectra"
        },
        "subcategories": {
            "6": 200
        }
    }
}

Then you change your C# class structure to the following:

public class Client
{
    public string Status { get; set; }
    public string Message { get; set; }
    public Data clientData { get; set; }
}

public class Data
{
    public int id { get; set;}
    public Dictionary<string, string> properties { get; set; }
    public Dictionary<string, int> subcategories { get; set; }
}

That should work (though I haven't tested), and will hopefully allow you to use it how you need to still.

NOTE: You could also move id and subcategories into the root, and keep clientData as a Dictionary<string, string>. All depends on your preference really, the important thing here is that you be careful not to mix types.

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

1 Comment

Thanks! My friend is changig JSON structure right now and I'll check it in few minutes
0

Json

{  
   "status":"ok",
   "message":"Dane klienta zostau0142y pobrane pomyu015blnie",
   "clientData":{  
      "id":22,
      "imie":"Pppppppppp",
      "nazwisko":"Ppppppppppppp",
      "tel":"111111126",
      "email":"[email protected]",
      "ulica":"Na Przyzbie",
      "nr_budynku":"3",
      "nr_lokalu":"41",
      "kod_pocztowy":"02-813",
      "miejscowosc":"Warszawa",
      "samochod_marka":"opel",
      "samochod_model":"vectra",
      "subcategories":{  
         "6":200
      }
   }
}

C# classes

public class Subcategories
{
    public int __invalid_name__6 { get; set; }
}

public class ClientData
{
    public int id { get; set; }
    public string imie { get; set; }
    public string nazwisko { get; set; }
    public string tel { get; set; }
    public string email { get; set; }
    public string ulica { get; set; }
    public string nr_budynku { get; set; }
    public string nr_lokalu { get; set; }
    public string kod_pocztowy { get; set; }
    public string miejscowosc { get; set; }
    public string samochod_marka { get; set; }
    public string samochod_model { get; set; }
    public Subcategories subcategories { get; set; }
}

public class RootObject
{
    public string status { get; set; }
    public string message { get; set; }
    public ClientData clientData { get; set; }
}

Note that root->clientData->subcategories->6 would result in invalid class name, as class names in C# can not begin with a number.


With hack fix:

For example:

public class DynamicDictionary : DynamicObject
{
    private readonly Dictionary<string, object> dictionary;

    public DynamicDictionary(Dictionary<string, object> dictionary)
    {
        this.dictionary = dictionary;
    }

    public override bool TryGetMember(
        GetMemberBinder binder, out object result)
    {
        return dictionary.TryGetValue(binder.Name, out result);
    }

    public override bool TrySetMember(
        SetMemberBinder binder, object value)
    {
        dictionary[binder.Name] = value;

        return true;
    }
}

Which can be used as follows:

dynamic x = new DynamicDictionary(
    new Dictionary<string, object> {{"Name", "Peter"}});

3 Comments

I can't make so many fields like imie, nazwisko, tel, email etc. It must be make dynamicaly because every time those fields are changing
Add the full list, they are not all required, but c# should have the definition.
Hack fix would be to use dynamic, but personally I do not prefer that in this case - you just delay your problems.
0

you can use Newtonsoft.Json - add a reference to your project and add the using directive

using Newtonsoft.Json;

        //then your code 
        dynamic ma_json = JsonConvert.DeserializeObject<dynamic>(json);
        //and then you can get say the id:
        var id = ma_json.clientData.id; 
        // ... do whatever you want with the id
        if (ma_json.clientData.id == 22) //evaluates to true in your case
        { 
           //do something
        }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.