0

I am using .net to call to a webservice then parse it to usable data.
Right now I am experimenting with this call: http://www.reddit.com/r/all.json Which returns: http://pastebin.com/AbV4yVuC This is put in to a string, which I called jsontxt.

I am using JSON.NET to parse the information but it doesn't seem to be working. I initially tried to deserialize it as an object and it didn't work as nothing is put in to the variable

Then I tried to deserialize it as a dataset and I'm having no luck again. My error was

An unhandled exception of type 'Newtonsoft.Json.JsonException' occurred in Newtonsoft.Json.dll

MY CODE:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace tutorialresult1
{

class Program
{
    static void Main(string[] args)
    {
        using (var webClient = new System.Net.WebClient())
        {
            var jsontxt = webClient.DownloadString("http://www.reddit.com/r/all.json");
           // Console.Write(json);

            // -----Deserializing by Object--------------
            //MediaEmbed account = JsonConvert.DeserializeObject<MediaEmbed>(jsontxt);
            //Console.WriteLine(account.width);   //COMES OUT TO NULL

            // -----Deserializing by DataSet--------------
            DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(jsontxt);
            DataTable dataTable = dataSet.Tables["Children"];
            Console.WriteLine(dataTable.Rows.Count);
        }

    }

    public class MediaEmbed
    {
        public string content { get; set; }
        public int width { get; set; }
        public bool scrolling { get; set; }
        public int height { get; set; }
    }
.... //rest of classes here for each json which were generated with http://json2csharp.com/
    }
}

I'm just trying to make the JSON easily accessible by parsing it.

1
  • In order to deserialize to a DataSet, the JSON data must be in a very specific format. Reddit data is not in that format. So, you either need to use strongly typed classes, as shown in Craig W's answer, or deserialize to a JObject or a dynamic variable. Here is an answer that explains all of this in a little more detail. Commented Jul 28, 2014 at 17:22

3 Answers 3

1

Using json2charp I generated the following set of classes. Using those you should be able to deserialize the JSON into RootObject using JSON.NET.

var account = JsonConvert.DeserializeObject<RootObject>(jsontxt);

.

public class MediaEmbed
{
    public string content { get; set; }
    public int? width { get; set; }
    public bool? scrolling { get; set; }
    public int? height { get; set; }
}

public class Oembed
{
    public string provider_url { get; set; }
    public string description { get; set; }
    public string title { get; set; }
    public string url { get; set; }
    public string type { get; set; }
    public string author_name { get; set; }
    public int height { get; set; }
    public int width { get; set; }
    public string html { get; set; }
    public int thumbnail_width { get; set; }
    public string version { get; set; }
    public string provider_name { get; set; }
    public string thumbnail_url { get; set; }
    public int thumbnail_height { get; set; }
    public string author_url { get; set; }
}

public class SecureMedia
{
    public Oembed oembed { get; set; }
    public string type { get; set; }
}

public class SecureMediaEmbed
{
    public string content { get; set; }
    public int? width { get; set; }
    public bool? scrolling { get; set; }
    public int? height { get; set; }
}

public class Oembed2
{
    public string provider_url { get; set; }
    public string description { get; set; }
    public string title { get; set; }
    public int thumbnail_width { get; set; }
    public int height { get; set; }
    public int width { get; set; }
    public string html { get; set; }
    public string version { get; set; }
    public string provider_name { get; set; }
    public string thumbnail_url { get; set; }
    public string type { get; set; }
    public int thumbnail_height { get; set; }
    public string url { get; set; }
    public string author_name { get; set; }
    public string author_url { get; set; }
}

public class Media
{
    public string type { get; set; }
    public Oembed2 oembed { get; set; }
}

public class Data2
{
    public string domain { get; set; }
    public object banned_by { get; set; }
    public MediaEmbed media_embed { get; set; }
    public string subreddit { get; set; }
    public string selftext_html { get; set; }
    public string selftext { get; set; }
    public object likes { get; set; }
    public SecureMedia secure_media { get; set; }
    public string link_flair_text { get; set; }
    public string id { get; set; }
    public int gilded { get; set; }
    public SecureMediaEmbed secure_media_embed { get; set; }
    public bool clicked { get; set; }
    public bool stickied { get; set; }
    public string author { get; set; }
    public Media media { get; set; }
    public int score { get; set; }
    public object approved_by { get; set; }
    public bool over_18 { get; set; }
    public bool hidden { get; set; }
    public string thumbnail { get; set; }
    public string subreddit_id { get; set; }
    public object edited { get; set; }
    public string link_flair_css_class { get; set; }
    public object author_flair_css_class { get; set; }
    public int downs { get; set; }
    public bool saved { get; set; }
    public bool is_self { get; set; }
    public string permalink { get; set; }
    public string name { get; set; }
    public double created { get; set; }
    public string url { get; set; }
    public object author_flair_text { get; set; }
    public string title { get; set; }
    public double created_utc { get; set; }
    public int ups { get; set; }
    public int num_comments { get; set; }
    public bool visited { get; set; }
    public object num_reports { get; set; }
    public object distinguished { get; set; }
}

public class Child
{
    public string kind { get; set; }
    public Data2 data { get; set; }
}

public class Data
{
    public string modhash { get; set; }
    public List<Child> children { get; set; }
    public string after { get; set; }
    public object before { get; set; }
}

public class RootObject
{
    public string kind { get; set; }
    public Data data { get; set; }
}
Sign up to request clarification or add additional context in comments.

7 Comments

It might be better to highlight that he needs to deserialize to the type RootObject. ie. RootObject account = JsonConvert.DeserializeObject<RootObject>(jsontxt);
I assumed the OP understood that as the code he posted showed he had a basic understanding of specifying the type to deserialize into.
Yes, but he also had access to the types you have displayed from json2csharp (judging by the comments next to his MediaEmbed class and failed to make the connection. :)
If I was to deserialize it, how would I access the data? Using a . won't work, like in account.title (That's just an example)
The same way you access any property of any object, with dot notation. account.data.after would get you the after property of the Data instance. If it's a collection (like children in Data) you would loop over it or you LINQ.
|
1

You are trying to deserialize a json-string into a dataset-object. But the json-string doesn't have the format of a dataset. So you need to create a class which matches the json or deserialize it into a dictionary or sth. like this.

Comments

0

Have you tried deserialize using the following? Seems to be more robust for me.

using System.Web.Script.Serialization;
...
var x = new JavaScriptSerializer().Deserialize<Obj_type>(jsonstring); 

http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer%28v=vs.110%29.aspx

as purplej0kr mentioned you will need to have a .Net model to use for Obj_type (and it will need to match the object you are parsing) or this will not work.

You will need the System.Web.Extensions.dll referenced in your project (right click add reference, dll is usually under 'Assemblies'). Otherwise: Where can I find the assembly System.Web.Extensions dll?

1 Comment

It says it cannot find a reference for 'using System.Web.Script.Serialization;'

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.