-1

I'm trying to parse data from my website and then deserializing it inside C# but I haven't gotten it work. What is the simplest way and method to use when grabbing data from a http source and deserializing it in C#?

Kraffs
  • 533
  • 2
  • 7
  • 22
  • Can you elaborate on "deserializing it"? It is JSON data, XML, HTML, etc.? – Brad Christie Jan 10 '11 at 03:55
  • Right now it's JSON data but I couldnt get it to work so I'm wondering what the best way would be to serialize data in php and deseralizing it in csharp. – Kraffs Jan 10 '11 at 03:58
  • Now, now, no need to down-vote without at least a comment as to why... –  Jan 10 '11 at 03:59
  • No reason why JSON (or any other format) shouldn't work. Just need to make sure the sides agree -- if it "doesn't work", *troubleshoot* and find out why :) C# has native support for both JSON and XML so it's a toss-up unless you step into the "Web Service" realm where, *provided a schema*, lots of "typed" scaffolding can be automatically generated. –  Jan 10 '11 at 04:01
  • I searched around on how to use JSON but I couldnt use the namespace for some reason, something about it not existing. – Kraffs Jan 10 '11 at 04:02
  • Try using [JavaScriptSerializer](http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx) in the System.Web.Script.Serialization library. You can generate an object (or objects) that mimic the JSON structure and deserialize to that (or use a dynamic variable as it will be a list of Dictionary values). – Brad Christie Jan 10 '11 at 04:05
  • I guess this error when trying to use the System.Web.Script.Serialization namespace: The type or namespace name 'Script' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) – Kraffs Jan 10 '11 at 04:16
  • @Kraffs: Did you include `System.Web.Extensions.dll`? – Brad Christie Jan 10 '11 at 04:30

2 Answers2

3

Based on the following JSON:

{
  name: "Brad Christie",
  score: 10,
  questions: [
    {
      question_id: 1,
      question: "How do I deserialize javascript?",
      answer: "JavaScriptSerializer",
      points: 10
    }
  ]
}

and assuming these classes:

public class Question
{
  public Int32 question_id;
  public String questions;
  public String answer;
  public Int32 points;
}

public class JSExample
{
  public String name;
  public Int32 score;
  IEnumerable<Question> questions;
}

the below should work (though didn't test and going by memory of what I've done in the past). Basically, the JavaScriptSerializer should take a JSON string and parse it out in to your custom object, or result in a dictionary of the structure of the JSON (I personally prefer placing it an object so i can manipulate it as I chose, but you can use the dictionary/dynamic variable and debug to see the result). Anyways, the code would be as follows:

//String the_JSON_string = <data from webpage>;
JavaScriptSerializer serializer = new JavaScriptSerializer();
JSExample example = serializer.Deserialize<JSExample>(the_JSON_string);
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • `answer: "JavaScriptSerializer", points: 10` ...ok, you've earned those :) Good example. (But you mised a comma there, fwiw) –  Jan 10 '11 at 04:25
  • lol, I get less subtle as it gets closer to bed time--good call. Was more just going for an outline and showing how the JavaScriptSerializer works more than anything. I'll correct it though, good catch. ;-) – Brad Christie Jan 10 '11 at 04:27
  • It says System.Web.Script.Serialization namespace Script does not exist. How do I solve this? – Kraffs Jan 10 '11 at 04:34
  • @Kraffs: As I mentioned in a comment on your question, have you included `System.Web.Extensions.dll`? if not, that would be the first place to look (right click your project, "add reference"). – Brad Christie Jan 10 '11 at 04:37
  • In what folder can I find System.Web.Extensions.dll? – Kraffs Jan 10 '11 at 04:39
  • You shouldn't have to, it should be in your .NET tab of the references dialog. What version are you compiling to? (right-click your project and select preferences) – Brad Christie Jan 10 '11 at 04:50
  • @Kraffs: Change it to .net 4 (without client profile). Make sure you save before switching as it will want to reload your project (which should be fine as it needs to make adjustments to the new target framework). Now the system.web.extensions library should be available. --- (If you're interested in what you're changing you can [look here](http://stackoverflow.com/questions/3579951/net-framework-4-0-client-profile-vs-net-framework-4-0), it shouldn't be an issue [though i don't know your application's intent]) – Brad Christie Jan 10 '11 at 05:04
0

There are a number of JSON serializers in .NET. Most notably the built in DataContractSerializer

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx

EDIT: (Sorry wrong link, here's the one for JSON http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx
and http://msdn.microsoft.com/en-us/library/bb410770.aspx)

and JSON.NET

http://json.codeplex.com/

Doobi
  • 4,844
  • 1
  • 22
  • 17
  • System.Runtime.Serialization is only for XML though isnt it? – Kraffs Jan 10 '11 at 04:15
  • No, it also does JSON. :) My bad - that's wrong link - here's the one for "DataContractJsonSerializer" http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx – Doobi Jan 10 '11 at 05:21