0

Recently I got an appointment where I need to download some data from Cloud using it's REST API and store the values from that "string" in an object or in some variables. The download is done, all I need to do now is to parse somehow the data.

Until now i made this kind of code:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("some link");

request.ContentType = "application/json; charset=utf-8";
request.Method = "GET";
request.Headers.Add("Carriots.apiKey", "key");
using (WebResponse response = request.GetResponse())
{
    using(Stream stream = response.GetResponseStream())
    {
        StreamReader reader = new StreamReader(stream);
        JObject json = JObject.Parse(reader.ReadToEnd());
        Console.WriteLine(json.ToString());
    }
}

And here is the output:

{
    "total_documents": 3,
    "result": [
    {
        "_id": "...",
        "protocol": "v1",
        "checksum": "",
        "_t": "str",
        "at": 1444134377,
        "device": "-just a device-",
        "data": {
            "field1": "123",
            "field2": "1536"
        },
        "id_developer": "....",
        "created_at": 1444134377,
        "owner": "-someUser-"
    }
    ]

}

I know that there are a lot of solutions on the internet, but none of them does what I need. Okay, I found something, but that iterates on every line and checks the values that way, but in my case I can have thousands of outputs like this.

Are there any ways to do all that (I mean the parsing) using some kind of built-in functions or the only solution is to iterate or write some regular expression?

Norbert
  • 25
  • 8
  • are you reading that JSON in c# or in js? – stackoverfloweth Oct 06 '15 at 14:48
  • Deserializing is what you are looking for! – Arghya C Oct 06 '15 at 14:50
  • Try Linq to Json http://www.newtonsoft.com/json/help/html/LINQtoJSON.htm – Matteo Oct 06 '15 at 14:50
  • Possibly related: [JObject.Parse vs JsonConvert.DeserializeObject](http://stackoverflow.com/questions/23645034/jobject-parse-vs-jsonconvert-deserializeobject) (`JObject.Parse` is Newtonsoft Json.Net) – crashmstr Oct 06 '15 at 14:52
  • Possible duplicated. Take a look at [this post](http://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp) – Triet Doan Oct 06 '15 at 14:53
  • i'm reading this on c#, and yes I need a deserialization, the problem is how – Norbert Oct 06 '15 at 14:59
  • You already have a `JObject` that you could work with ([JObject Documentation](http://www.newtonsoft.com/json/help/html/t_newtonsoft_json_linq_jobject.htm)). The other option is to have concrete classes to deserialze to. Either way should work. (I guess I would ask how you got to using `JObject.Parse` without knowing that you had data to work with). – crashmstr Oct 06 '15 at 15:00

1 Answers1

-1

assuming the json will always follow this format, define your model

public class jsonData{
    public int total_documents {get;set;}
    public resultData result {get;set;}
}

public class resultData{
    public int _id {get;set;}
    public string protocol {get;set;}
    ....
}

then use deserialize

Json.Deserialize<jsonData>(yourstring)
stackoverfloweth
  • 6,669
  • 5
  • 38
  • 69
  • Thank you, with this i think I can manage something. All I needed to this was a some runtime deserialization: [link](http://blog.anthonybaker.me/2013/05/how-to-consume-json-rest-api-in-net.html) – Norbert Oct 06 '15 at 15:00