Trip Trip - 1 year ago 75
JSON Question

Converting a string to JSON in C#

I'm trying to use Simple JSON to convert this string to JSON :


Unfortunately, it appears that Visual Studio doesn't have Interactive Debugging Console. As in, placing a debugger on a line, and stepping into that part of the code in a live interactive console. Where I would otherwise be able to experiment with SimpleJSON's library and see how to make this work. By all means, correct me if I'm wrong!

Being that that's impossible though, would anyone know how to accomplish this? I have tried this :

JSONData jsonData = new JSONData(my_json_string);

But that escapes the string even more and keeps it a string :


I'm new to C#, but I'm surprised there's nothing native to C# that would make something as common as parsing JSON more accessible. Is there one?

Answer Source

First, create your data model. You can use json2sharp, very helpful tool.

public class Item
    public int id { get; set; }
    public string title { get; set; }
    public int position_x { get; set; }
    public int position_y { get; set; }
    public int position_z { get; set; }
    public int rotation_x { get; set; }
    public int rotation_y { get; set; }
    public int rotation_z { get; set; }
    public string created { get; set; }

Next use Newtonsoft.Json and call deserialize method.

var list = JsonConvert.DeserializeObject<List<Item>>(Yourjson);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download