问题描述
我正在尝试使用简单JSON 将此字符串转换为JSON: /p>
I'm trying to use Simple JSON to convert this string to JSON :
"{\"objects\":[{\"id\":1,\"title\":\"Book\",\"position_x\":0,\"position_y\":0,\"position_z\":0,\"rotation_x\":0,\"rotation_y\":0,\"rotation_z\":0,\"created\":\"2016-09-21T14:22:22.817Z\"},{\"id\":2,\"title\":\"Apple\",\"position_x\":0,\"position_y\":0,\"position_z\":0,\"rotation_x\":0,\"rotation_y\":0,\"rotation_z\":0,\"created\":\"2016-09-21T14:22:52.368Z\"}]}"
不幸的是,似乎Visual Studio没有Interactive Debugging Console.与之类似,将调试器放在一行上,然后在实时交互式控制台中进入代码的这一部分.在其他方面,我将能够使用SimpleJSON的库进行实验,并了解如何使它工作.如果我错了,一定要纠正我!
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 :
"\"{\\\"objects\\\":[{\\\"id\\\":1,\\\"title\\\":\\\"Book\\\",\\\"position_x\\\":0,\\\"position_y\\\":0,\\\"position_z\\\":0,\\\"rotation_x\\\":0,\\\"rotation_y\\\":0,\\\"rotation_z\\\":0,\\\"created\\\":\\\"2016-09-21T14:22:22.817Z\\\...
我是C#的新手,但令我感到惊讶的是,C#本身没有什么东西比解析JSON更为常见.有一个吗?
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?
推荐答案
首先,创建数据模型.您可以使用非常有用的工具 json2sharp .
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; }
}
接下来使用Newtonsoft.Json
并调用反序列化方法.
Next use Newtonsoft.Json
and call deserialize method.
var list = JsonConvert.DeserializeObject<List<Item>>(Yourjson);
这篇关于在C#中将字符串转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!