1

hi i want to read this json string into my local variables in c# anyone can tell how do i do it

{  
   "type":"object",
   "error":"",
   "warning":"",
   "info":"",
   "response_code":"200",
   "response":[  
      {  
         "MeterID":"6",
         "KWHMainActual":"0.0",
         "KWActual":"0 ",
         "KWHDGActual":"0.2",
         "MeterOnOff":"1",
         "MeterDefective":"0",
         "RecordDateTime":"2016-08-17 12:46:49.000"
      }
   ]
}
M. Wiśnicki
  • 6,094
  • 3
  • 23
  • 28
R-T
  • 113
  • 1
  • 8
  • 5
    Duplicate: http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object – McNets Dec 23 '16 at 07:29
  • 1
    Possible duplicate of [Deserialize JSON with C#](http://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp) – Circle Hsiao Dec 23 '16 at 07:33

1 Answers1

1

To generate classes you can use JSON2CSHARP and to desrialize your JSON use Json.NET.

Model:

 public class Response
    {
        public string MeterID { get; set; }
        public string KWHMainActual { get; set; }
        public string KWActual { get; set; }
        public string KWHDGActual { get; set; }
        public string MeterOnOff { get; set; }
        public string MeterDefective { get; set; }
        public string RecordDateTime { get; set; }
    }

    public class RootObject
    {
        public string type { get; set; }
        public string error { get; set; }
        public string warning { get; set; }
        public string info { get; set; }
        public string response_code { get; set; }
        public List<Response> response { get; set; }
    }

and deserialize method

JsonConvert.DeserializeObject<RootObject>("jsonString");

Edit:

Using WebRequest You can get your json from uri, try this method

public async Task<string> GetFromUri(string uri)
    {
        var request = (HttpWebRequest) WebRequest.Create(new Uri(uri));
        request.ContentType = "application/json";
        request.Method = "GET";

        // Send the request to the server and wait for the response:  
        using (var response = await request.GetResponseAsync())
        {
            // Get a stream representation of the HTTP web response:  
            using (var stream = response.GetResponseStream())
            {
                var reader = new StreamReader(stream);
                var message = JsonConvert.DeserializeObject<string>(reader.ReadToEnd());
                return message;
            }
        }
    }

call method

 var jsonString = await GetFromUri("uri");
M. Wiśnicki
  • 6,094
  • 3
  • 23
  • 28
  • i m using newtonsoft.json and json string is coming from an URL can u explain how to get json string from url and put it in variables – R-T Dec 23 '16 at 08:02
  • @R-T Ye sure, look at edit. you can achieve it using WebRequest. I add sample method wich you can use to call as i show. – M. Wiśnicki Dec 23 '16 at 08:13
  • [{"MeterID":"6","KWHMainActual":"0.0","KWActual":"0 ","KWHDGActual":"0.2","MeterOnOff":"1","MeterDefective":"0","RecordDateTime":"2016-08-17 12:46:49.000"}]} can anyone tell array number for KWHMainActual like array number is 0 for MeterID – R-T Dec 24 '16 at 10:54
  • @R-T You can't ask about all of your problem in one question or comment, try resolve your problem by yourself. Get knowledge about JSON it will be very helpfull in future programming. All of things what you asking someone asked before you and answer exist on this forum. If this answer help you mark as correct. – M. Wiśnicki Dec 24 '16 at 11:00
  • i want to know position of KWHMainActual but there is no help on google how to read position of JSON string – R-T Dec 24 '16 at 11:06