首页 > 解决方案 > 如何用 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;
    }
}

标签: c#jsonparsingunity3djson.net

解决方案


使用 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));
        }

推荐阅读