c# - 将 LitJson 转换为 Newtonsoft Json 以在 IOS 设备 Unity C# 上运行
问题描述
如何在统一 c# 中将 LitJson Json 转换为 NewtonSoft Json?
例子 :
在利特森
JsonData CJsonData;
cJsonData = JsonMapper.ToObject(www.downloadHandler.text);
Debug.log(cJsonData["reason"].ToString();
// 这个 cJsonData 可以包含一个嵌套数组。
ios 的 Newtonsoft Json 中的代码如何?
我不想创建类属性,因为 www.donwloadHandler.text 的返回可能不同。这取决于回报。当使用具有数据类型 JsonData 的 LitJson 并使用 JsonMapper.Tobject 时,我可以轻松获取数据而无需更多代码。
*
在 LitJson 中,我们有 DataType JsonData,它会自动将其转换为来自 Mapper 的关联数组。
*
我希望我能得到像 LitJson 这样的数据
Debug.log(cJsonData["reason"].ToString();
或者可能
Debug.log(cJsonData["reason"]["abc"].ToString();
或者可能
Debug.log(cJsonData["reason"]["cc"]["aaa"].ToString();
但是在 newtonsoft json 中,我们必须添加一个类来反序列化对象。
在 newtonsoft json 中:
someclass Json = JsonConvert.DeserializeObject<someclass>(www.donwloadHandler.text);
这是我不想要的。因为我们需要添加一些类
和这个 :
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();
}
}
推荐阅读
- flutter - Flutter:更改 ListView.builder 中选定按钮的背景颜色
- talend - 如何使用 Talend 获取两列之间的差异
- c - 小波变换 opencl for 循环
- neo4j - Neo4j Cypher - 带子句查询
- wordpress - Woocommerce,使用网络挂钩和数据直接共享更新和列出新产品
- javascript - 将 html 传递到引导工具提示时出现“SafeValue must use [property]=binding”错误
- c++ - sizeof 内部的函数调用不会调用它?C++
- reactjs - React & SASS 项目如何移除循环引用 (@use)
- jenkins - Jenkins 管道 perfReport:使用“专家模式”约束
- owncloud - 如何在保留其公共链接的同时替换 Owncloud 中的现有文件?