0

I'm looking for a simple example of calling a feed from a url and then looping through the data pulling the values out in C#.

I'm managed to get the feed data into a string variable like this. I've looked at the newtonsoft.Json dll but can't find a simple example of pulling the data out. The data isn't complex and i've added it to the bottom.

so basically _feedData now contains my JSON data I somehow like to convert it into a JSON object then foreach it pulling the values out.

        static void Main(string[] args)
    {

        string _feedData = GetJSONFeed();


    }
     public static string GetJSONFeed()
    {
        string formattedUri = "http://www.myJsonFeed.com/blah.json";

        HttpWebRequest webRequest = GetWebRequest(formattedUri);
        HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
        string jsonResponse = string.Empty;
        using (StreamReader sr = new StreamReader(response.GetResponseStream()))
        {
            jsonResponse = sr.ReadToEnd();
        }
        return jsonResponse;
    }

    private static HttpWebRequest GetWebRequest(string formattedUri)
    {
        // Create the request’s URI.
        Uri serviceUri = new Uri(formattedUri, UriKind.Absolute);

        // Return the HttpWebRequest.
        return (HttpWebRequest)System.Net.WebRequest.Create(serviceUri);
    }

my JSON data is like this:

[
 {
    "id": "9448",
    "title": "title title title",
    "fulltext": "main body text",
   "url": "http://www.flikr.co.uk?id=23432"
 },
 {
    "id": "9448",
    "title": "title title title",
    "fulltext": "main body text",
   "url": "http://www.flikr.co.uk?id=23432"
 }
]

thanks for any help. Rob

Nix
  • 57,072
  • 29
  • 149
  • 198
Rob
  • 1
  • 1
  • This post has everything you need: http://stackoverflow.com/questions/1212344/parse-json-in-c – Nix Oct 30 '10 at 15:37

3 Answers3

2

Follow the json-net project:

For situations where you are only interested in getting values from JSON, don't have a class to serialize or deserialize to, or the JSON is radically different from your class and you need to manually read and write from your objects then LINQ to JSON is what you should use. LINQ to JSON allows you to easily read, create and modify JSON in .NET.

And the LINQ to JSON Example:

string json = @"{
  ""Name"": ""Apple"",
  ""Expiry"": new Date(1230422400000),
  ""Price"": 3.99,
  ""Sizes"": [
    ""Small"",
    ""Medium"",
    ""Large"
  ]
}";

JObject o = JObject.Parse(json);
string name = (string)o["Name"];
// Apple
JArray sizes = (JArray)o["Sizes"];
string smallest = (string)sizes[0];
// Small
gimel
  • 83,368
  • 10
  • 76
  • 104
0

Create a class to hold an instance of the feed data.

public class FeedData
{
    public string id { get; set; }
    public string title { get; set; }
    public string fulltext { get; set; }
    public string url { get; set; }
}

Then after you have the json response as a string deserialize it in to a list of FeedData objects.

var serializer = new DataContractJsonSerializer(typeof(List<FeedData>));
using (var stream = new MemoryStream(Encoding.Unicode.GetBytes(jsonResponse)))
{
   var serializer = new DataContractJsonSerializer(typeof(List<FeedData>));
   return serializer.ReadObject(ms) as List<FeedData>;
}

Note that your return value should now be List<FeedData> or IEnumerable<FeedData>

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
0

Rather than a JSON Object, I guess you need to parse the JSON String to CLR Object, right?

This documentation page mentioned this.

xandy
  • 27,357
  • 8
  • 59
  • 64