1

I am new to json. This is my json object.Anyone please help me to create C# class for the below json object

{
   "JBS" : {
      "name" : "Jobsite"
   },
   "LNK" : {
      "name" : "Linked IN"
   },
   "MUK" : {
      "name" : "Monster UK"
   }
}

In that I need information of (JBS,jobsite ) like that for n elements.

Adrian P
  • 6,479
  • 4
  • 38
  • 55
Lax_me
  • 307
  • 1
  • 7
  • 20
  • this could be found online so easily I suggest you to make some research like here http://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object – meda Sep 14 '13 at 05:48
  • Does each object have only property "name" ? – Ronak Patel Sep 14 '13 at 05:51
  • but in my json example "JBS","LNK","MUK" are changing every time .some times I may get n codes so how can I create dynamic class with the names like JBS","LNK","MUK" for saving n values. – Lax_me Sep 14 '13 at 05:52
  • yes patel, each object has 'name' property only. but the codes( "JBS","LNK","MUK" ) will change. – Lax_me Sep 14 '13 at 05:53
  • If its possible you should be using structure like { "JBS" : "Jobsite", "LNK" : "Linked IN", "MUK" : "Monster UK" } this would be easier to parse – Ronak Patel Sep 14 '13 at 05:58
  • no Patel ,I'm getting this type of response from another webservice. so I cannot change this format. – Lax_me Sep 14 '13 at 06:04

3 Answers3

5

Declare classes as:

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

public class JsonData
{
    public Entity JBS{get;set;}
    public Entity LNK{get;set;}
    public Entity MUK{get;set;}
}

then use:

var json =
   @"{'JBS' : {'name' : 'Jobsite'},
   'LNK' : {'name' : 'Linked IN'},
   'MUK' : {'name' : 'Monster UK'}}";


JsonData jsonData = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<JsonData>(json);

}

UPDATE

I think this snippet lays out under your requirements.

var json =
@"{ 'JBS' : {'name' : 'Jobsite'},
    'LNK' : {'name' : 'Linked IN'},
    'MUK' : {'name' : 'Monster UK'}}";


var jsonData = new System.Web.Script.Serialization.JavaScriptSerializer()
.Deserialize<Dictionary<string,Dictionary<string, string>>>(json)
.ToDictionary(d => d.Key, d => d.Value.First().Value);
Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68
  • thanks. but my problem is that I need to capture what are the available jobsites in list like JBS=Jobsite,LNK=Linked IN ..... so that I can pass this information to users that these are the accessible jobsites for your credentials. – Lax_me Sep 14 '13 at 06:30
  • Do you have the full list of sites? – Hamlet Hakobyan Sep 14 '13 at 06:35
  • no depending upon the login credentials I'll get the job site name and job code. I want to capture these values as code=name (key/ value pair). – Lax_me Sep 14 '13 at 06:39
2

If you want to enumerate the result, you should try deserializing into a dictionary. Here's an example that deserializes it and then makes a list of them:

var json = "{ \"JBS\" : { \"name\" : \"Jobsite\" }, \"LNK\" : { \"name\" : \"Linked IN\" }, \"MUK\" : { \"name\" : \"Monster UK\" } }";

var result = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Dictionary<string, JobSite>>(json);

var jobsites = new List<JobSite>(result.Count);

foreach (var pair in result)
{
    var jobsite = pair.Value;
    jobsite.Short = pair.Key;
    jobsites.Add(jobsite);
}

The example uses a class looking like this:

public class JobSite
{
    public string Short { get; set; }
    public string Name { get; set; }
}
0

Try using json.net to deserialize the json into a dynamic object. Then you should be able to access the properties.

You can also refer to this question: Deserialize json object into dynamic object using Json.net

Community
  • 1
  • 1
Jason
  • 15,915
  • 3
  • 48
  • 72