将 LitJson 转换为 Newtonsoft Json for 运行 at IOS device Unity C#

Convert LitJson to Newtonsoft Json for run at IOS device Unity C#

如何在 unity c# 中将 LitJson Json 转换为 NewtonSoft Json?

示例:

点亮Json

JsonData CJsonData;
cJsonData = JsonMapper.ToObject(www.downloadHandler.text);
Debug.log(cJsonData["reason"].ToString();

// 这个cJson数据可以包含嵌套数组。

代码在 Newtonsoft Json for ios 中的样子如何?

我不想创建 class 属性,因为 www.donwloadHandler.text 中的 return 可能不同。它取决于return。 当使用数据类型为 JsonData 的 LitJson 并使用 JsonMapper.Tobject 时,我可以轻松获取数据而无需更多代码。

*

In LitJson we have DataType JsonData which is automatic convert it to an associated array from Mapper.

*

我希望我能得到像 Lit 这样的数据Json

Debug.log(cJsonData["reason"].ToString();

或者也许

Debug.log(cJsonData["reason"]["abc"].ToString();

或者可能

Debug.log(cJsonData["reason"]["cc"]["aaa"].ToString();

但是在 newtonsoft json 我们必须添加一个 class 来反序列化对象。

在 newtonsoft json 中:

someclass Json = JsonConvert.DeserializeObject<someclass>(www.donwloadHandler.text);

我不想要这个。因为我们需要添加一些class

还有这个:

string data = JsonConvert.DeserializeObject(www.downloadHanlder.text);

这我也不想要。因为它是字符串而不是像 litjson.

这样的关联数组

清楚了吗?

谢谢

大同小异。无需反序列化读取数据,只需使用一个 JObject:

using System;
using Newtonsoft.Json.Linq;

public class Program
{
    public static void Main()
    {
        string json = @"
            {
              ""CPU"": ""Intel"",
              ""Integrated Graphics"": true,
              ""USB Ports"": 6,
              ""OS Version"": 7.1,
              ""Drives"": [
                ""DVD read/writer"",
                ""500 gigabyte hard drive""
              ],
              ""ExtraData"" : {""Type"": ""Mighty""}
            }";

        JObject o = JObject.Parse(json);

        Console.WriteLine(o["CPU"]);
        Console.WriteLine();
        Console.WriteLine(o["Drives"]);
        Console.WriteLine();
        Console.WriteLine(o["ExtraData"]["Type"]);

        Console.ReadLine();
    }
}