0
WebRequest request = WebRequest.Create("url" + result + "url");
string json;
var response = request.GetResponse();
request.ContentType = "application/json; charset=utf-8";

using (var streamr = new StreamReader(response.GetResponseStream()))
{
    json = streamr.ReadToEnd();
    MessageBox.Show(json);
}

I have this code that gets me the following sample string

[{"Type":1,"Country":"CA","Channel":"","Code":"1EZ","Start":"2014-10-24T00:00:00","End":"2015-10-23T00:00:00"},{"Type":2,"Country":"","Channel":"","Code":"UAD","Start":"2014-10-24T00:00:00","End":"2017-10-23T00:00:00"},{"Type":2,"Country":"","Channel":"","Code":"TPQ","Start":"2014-10-24T00:00:00","End":"2017-10-23T00:00:00"},{"Type":3,"Country":"","Channel":"","Code":"SVC_PRIORITY","Start":"2014-10-24T00:00:00","End":"2017-10-23T00:00:00"}]

But I want to only get the Start & End Values - Is there a JSON interpreter that can do this?

3
  • 1
    There are plenty of them. The most common seems to be JSON.net newtonsoft.com/json. Also available via nuget Commented Mar 21, 2016 at 14:32
  • sweet, what would be the best way with json.net to parse my string? Serialize, Deserialize, Linq? I'll read up about it more but if you know it I may aswell ask Commented Mar 21, 2016 at 14:36
  • 1
    If you look at the examples given on the site, it should be quite clear, you need to deserialize a string to an object. Commented Mar 21, 2016 at 14:38

2 Answers 2

4

Something to get you started. Deserialize your json to a class using JSON.NET:

var jsonStr = "[{\"Type\":1,\"Country\":\"CA\",\"Channel\":\"\",\"Code\":\"1EZ\",\"Start\":\"2014 - 10 - 24T00: 00:00\",\"End\":\"2015 - 10 - 23T00: 00:00\"},{\"Type\":2,\"Country\":\"\",\"Channel\":\"\",\"Code\":\"UAD\",\"Start\":\"2014 - 10 - 24T00: 00:00\",\"End\":\"2017 - 10 - 23T00: 00:00\"},{\"Type\":2,\"Country\":\"\",\"Channel\":\"\",\"Code\":\"TPQ\",\"Start\":\"2014 - 10 - 24T00: 00:00\",\"End\":\"2017 - 10 - 23T00: 00:00\"},{\"Type\":3,\"Country\":\"\",\"Channel\":\"\",\"Code\":\"SVC_PRIORITY\",\"Start\":\"2014 - 10 - 24T00: 00:00\",\"End\":\"2017 - 10 - 23T00: 00:00\"}]";
var myObjectList = JsonConvert.DeserializeObject<List< MyObject>>(jsonStr);

And your class example:

public class MyObject
{
    public string Type { get; set; }
    public string Country { get; set; }
    public string Channel { get; set; }
    public string Code { get; set; }
    public string Start { get; set; }
    public string End { get; set; }
}
Sign up to request clarification or add additional context in comments.

6 Comments

from this point how do I get Start & End values? DateTime start = MyObject.Start? -- doesnt work :s
You can parse the dates using: var date = DateTime.Parse(MyObject.Start); Which will parse your string dates to c# datetime object.
I am getting an error 'An object reference is required for the nonstatic field, method, or property Form1.MyObject.Start' preventing me from doing this. I have my code in public Form1() and MyObject class outside of it
You are trying to access your class property in a static way and it is not static. You need a concrete implementation of the class to be able to access it. e.g. var myobject = new MyObject { Start = "21-03-2016" }; var date = DateTime.Parse(myobject.Start);
Hmm... but I do not want a static value? I have multiple URL's I want to loop through with similar but different Start and End Dates. II want to display the Start & End values from the url so they cant have the same values. Im a little new to programming so sorry if I am not catching onto something I should be.
|
4

With http://json2csharp.com/ generate class from json:

public class RootObject
{
    public int Type { get; set; }
    public string Country { get; set; }
    public string Channel { get; set; }
    public string Code { get; set; }
    public string Start { get; set; }
    public string End { get; set; }
}

Then add JSON.NET from Nuget.

And deserialize json to generated object:

List<RootObject> o = JsonConvert.DeserializeObject<List<RootObject>>(json);

And get interesting properties.

1 Comment

This will only do a single object. Most likely will require a list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.