简体   繁体   English

在C#中将字符串转换为JSON

[英]Converting a string to JSON in C#

I'm trying to use Simple JSON to convert this string to JSON : 我正在尝试使用Simple JSON将此字符串转换为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\"}]}"

Unfortunately, it appears that Visual Studio doesn't have Interactive Debugging Console. 不幸的是,Visual Studio似乎没有交互式调试控制台。 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. 否则,我将能够尝试使用SimpleJSON的库,并了解如何使其工作。 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\\\...

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. 我是C#的新手,但我很惊讶C#没有任何原生内容可以解析JSON更易于访问的内容。 Is there one? 有吗?

First, create your data model. 首先,创建数据模型。 You can use json2sharp , very helpful tool. 你可以使用json2sharp ,非常有用的工具。

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. 接下来使用Newtonsoft.Json并调用deserialize方法。

var list = JsonConvert.DeserializeObject<List<Item>>(Yourjson);

The question asks how to convert a string to a JSON object... This can be achieved without using a Class or data model, as follows: 问题是如何将字符串转换为JSON对象...这可以在不使用类或数据模型的情况下实现,如下所示:

using Newtonsoft.Json;

string str = "{\"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\"}]}";
dynamic json = JsonConvert.DeserializeObject(str);

Now, you can access the json contents as follows: 现在,您可以按如下方式访问json内容:

json["objects"][0]["title"];

returns "Book"

One option for an "Interactive Debugging Console" where you can play around with C# code is Xamarin Workbooks ... microsoft.com/en-us/xamarin/tools/workbooks/ 可以使用C#代码的“交互式调试控制台”的一个选项是Xamarin工作簿 ... microsoft.com/en-us/xamarin/tools/workbooks/

Xamarin Workbooks provide a blend of documentation and code that is perfect for experimentation, learning, and creating... blah blah blah Xamarin工作簿提供了完整的文档和代码,非常适合实验,学习和创建......等等等等等等等等等等等等等。

Did you try system utilities? 你尝试过系统工具吗?

Like this one https://msdn.microsoft.com/ru-ru/library/system.json.jsonvalue.parse%28v=vs.95%29.aspx 喜欢这个https://msdn.microsoft.com/ru-ru/library/system.json.jsonvalue.parse%28v=vs.95%29.aspx

You can use 您可以使用

public static JsonValue Parse(string jsonString)

from JsonValue class and cast then to jsonobject or anything you want. JsonValue类,然后转换为jsonobject或任何你想要的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM