user3704986
user3704986

Reputation: 37

JSON Web Request string to parsed data with JSON.NET

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.

Upvotes: 0

Views: 2808

Answers (3)

kingPuppy
kingPuppy

Reputation: 3047

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?

Upvotes: 0

Craig W.
Craig W.

Reputation: 18165

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; }
}

Upvotes: 1

latonz
latonz

Reputation: 1750

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.

Upvotes: 1

Related Questions