1

Hello all I am new to JSON deserialize. this is the JSON data which has to be deserialized to .net objects so that I can store those values from JSON in the database.

This my code:

var client = newRestClient("https:xxxxxxxxxxxxxxxxxx/pincodes/");
var request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"username\"\r\n\r\xxxxxxxxxxxxxxxx\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\xxxxxxxxxxxxxxxx\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
string JsonContent = response.Content;

this how json file looks like:

[{"city": "AMBALA", "state": "Haryana", "city_type": "", "active": true, "route": "HR/I1H/ABA", "date_of_discontinuance": "", "state_code": "HR", "pincode": 134003, "city_code": "ABA", "dccode": "ABA"}, 

{"city": "AMBALA", "state": "Haryana", "city_type": "", "active": true, "route": "HR/I1H/ABA", "date_of_discontinuance": "", "state_code": "HR", "pincode": 134002, "city_code": "ABA", "dccode": "ABA"}]

I want to access specific values ex. value of city, pincodes etc. How to create a model, I tried but getting some error: "Error CS0825 The contextual keyword 'var' may only appear within a local variable declaration or in script code"

nadir hamidou
  • 423
  • 2
  • 7
  • 18
Abdul
  • 176
  • 3
  • 19
  • 3
    Possible duplicate of [Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)](https://stackoverflow.com/questions/4749639/deserializing-json-to-net-object-using-newtonsoft-or-linq-to-json-maybe) – Leo Bartkus Jan 27 '19 at 08:13

3 Answers3

3

The best way is use Json.NET.

string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";

Movie m = JsonConvert.DeserializeObject<Movie>(json);

string name = m.Name; // Bad Boys
AmirNorouzpour
  • 1,119
  • 1
  • 11
  • 26
2

You can use Json.Net for deserializing. The first step would be to define a model for your City.

For example,

public class CityDetail
{
    public string city { get; set; }
    public string state { get; set; }
    public string city_type { get; set; }
    public bool active { get; set; }
    public string route { get; set; }
    public string date_of_discontinuance { get; set; }
    public string state_code { get; set; }
    public int pincode { get; set; }
    public string city_code { get; set; }
    public string dccode { get; set; }
}

Now, you can use Json.Net to deserialize data as following.

var result = JsonConvert.DeserializeObject<List<CityDetail>>(jsonString);

This would give you a List with your data

Output enter image description here

Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
1

You can have a City Model and then Deserialize to that model.

 public class CityModel
    {
        public string city { get; set; }
        public string state { get; set; }
        public string city_type { get; set; }
        public bool active { get; set; }
        public string route { get; set; }
        public string date_of_discontinuance { get; set; }
        public string state_code { get; set; }
        public int pincode { get; set; }
        public string city_code { get; set; }
        public string dccode { get; set; }
    }


string JsonResult = @"[{'city': 'AMBALA', state: 'Haryana', 'city_type': '', 'active': true, 'route': 'HR / I1H / ABA', 'date_of_discontinuance': '', 'state_code': 'HR', 'pincode': 134003, 'city_code': 'ABA', 'dccode': 'ABA'},{ 'city': 'AMBALA', 'state': 'Haryana', 'city_type': '', 'active': true, 'route': 'HR/I1H/ABA', 'date_of_discontinuance': '', 'state_code': 'HR', 'pincode': 134002, 'city_code': 'ABA', 'dccode': 'ABA'}]";

var result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<CityModel>>(JsonResult);  

ListOfCity

Else you can use dynamic but that is costly.

List<dynamic> result = JsonConvert.DeserializeObject<List<dynamic>>(JsonResult);
var city = result[0].city;
Idris
  • 351
  • 1
  • 9
  • But I am unable to create a model I am new to these concepts – Abdul Jan 27 '19 at 09:56
  • i liked your result using the model but for beginers we should try the dynamic as it's mentioned in my proposition :D – nadir hamidou Jan 28 '19 at 07:52
  • 1
    @Nadir, we should use dynamic only in case if we don't know what object type is returned. If we are sure about the type of list returned we should always go with Model approach. Use of dynamic is considered as bad practice as it refers to type late binding, which means the system will check type only during execution instead of during compilation. [link](https://stackoverflow.com/questions/31859016/is-the-use-of-dynamic-considered-a-bad-practice) – Idris Jan 28 '19 at 10:07