c# - 如何用 C# 重新构造嵌套的 json 数据?或团结
问题描述
由于 Unity 的 JSONUtility.FromJSON 不支持结构 #1 中的嵌套对象,我正在尝试获取 JSON 数据(结构 #1)并将其重新构建为可在 Unity(结构 #2)中使用的可行结构。
有什么办法可以使用 Json.Net C# 重新构造 #1 并使其看起来像 #2?
结构 #1
这个 JSON 数据文件有 30,000 个对象,这些对象具有随机节点编号,经纬度坐标,这只是一小部分
{
"71": {
"longitude": -179.5,
"latitude": -19.5
},
"72": {
"longitude": -179.5,
"latitude": -18.5
},
"157": {
"longitude": -179.5,
"latitude": 66.5
},
"158": {
"longitude": -179.5,
"latitude": 67.5
}
}
变成这个...
结构 #2
[
{
"nodeId": 71,
"longitude": -179.5,
"latitude": -19.5
},
{
"nodeId": 72,
"longitude": -179.5,
"latitude": -18.5
},
{
"nodeId": 157,
"longitude": -179.5,
"latitude": 66.5
},
{
"nodeId": 158,
"longitude": -179.5,
"latitude": 67.5
}
]
如果可以进行此转换,以下是我要使用的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpatialMapper : MonoBehaviour
{
TextAsset jsonObj;
private void Start()
{
MapPoints();
}
public void MapPoints()
{
jsonObj = Resources.Load("LocationNodes") as TextAsset;
string localJsonData = jsonObj.text;
JSONObjectContainer jsonObjects = JsonUtility.FromJson<JSONObjectContainer>(localJsonData);
foreach (var item in jsonObjects.rootNodes)
{
print(item.NodeId);
}
}
[System.Serializable]
public class JSONObjectContainer
{
public RootNode[] rootNodes;
}
[System.Serializable]
public class RootNode
{
public string NodeId;
public NodeData nodeData;
}
[System.Serializable]
public class NodeData
{
public string latitude;
public string longitude;
}
}
解决方案
使用 JSON.NET,由于属性名称(您的nodeId
值)是动态的,您可以将其读入字典。然后,您可以遍历字典并创建正确格式的列表,然后对其进行序列化:
const string sourceJson = "{ \"71\": { \"longitude\": -179.5, \"latitude\": -19.5 }, \"72\": { \"longitude\": -179.5, \"latitude\": -18.5 }, \"157\": { \"longitude\": -179.5, \"latitude\": 66.5 }, \"158\": { \"longitude\": -179.5, \"latitude\": 67.5 }}";
private class OldEntry {
public double longitude { get; set; }
public double latitude { get; set; }
}
private class NewEntry {
public int nodeId { get; set; }
public double longitude { get; set; }
public double latitude { get; set; }
}
static void Main(string[] args) {
var oldData = JsonConvert.DeserializeObject<Dictionary<string, OldEntry>>(sourceJson);
var newData = new List<NewEntry>(oldData.Count);
foreach (var kvp in oldData) {
newData.Add(new NewEntry() {
nodeId = int.Parse(kvp.Key),
longitude = kvp.Value.longitude,
latitude = kvp.Value.latitude
});
}
Console.WriteLine(JsonConvert.SerializeObject(newData, Formatting.Indented));
}
推荐阅读
- algorithm - 检查静态数组是否不包含给定范围的元素的数据结构
- python - 如何使用`openpyxl`库在Excel中的合并单元格中写入?
- javascript - 使用请求的异步调用并让回调在函数中工作
- excel - 邮件与多个字段合并 - 需要使用逗号和“和”创建列表
- javascript - 有没有办法在 Three.js 中拥有透明的视频背景?
- c - 未显示最低值
- vue.js - 为什么卡片悬停在容器外?
- sql - SQLite 更新与特定值匹配的所有列值
- tensorflow - TensorRT GA 和 RC 有什么区别?
- javascript - 带有 Rxjs 问题的 Redux 可观察史诗