如何使用C#重新构造嵌套的json数据?或Unity

时间:2019-05-08 23:22:36

标签: c# json parsing unity3d json.net

由于Unity的JSONUtility,我试图将JSON数据(结构1)重新构造为可用于Unity(结构2)的可用结构.FromJSON不支持结构1中的嵌套对象。

有没有办法使用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;
    }
}

1 个答案:

答案 0 :(得分:1)

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