Jackson Barnes
Jackson Barnes

Reputation: 51

How to re-structure nested json data with C#? or Unity

I'm trying to take JSON data (Structure #1) and re-structure it into a workable structure to use in Unity (Structure #2) because of Unity's JSONUtility.FromJSON does not support nested objects in structure #1.

Is there any way to maybe use Json.Net C# to re-structure #1 and make it look like #2?

Structure #1

This JSON data file has 30,000 objects with random node numbers with lat and long coordinates this is only a small section

{
  "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
  }
}

Turn into this...

Structure #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      
   }
]

Below is the code I want to use if this conversion is possible

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;
    }
}

Upvotes: 0

Views: 68

Answers (1)

Tim
Tim

Reputation: 6060

Using JSON.NET, since the property names (your nodeId values) are dynamic, you could read it into a dictionary. You could then loop through the dictionary and create a list of the correct format, then serialize that:

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

Upvotes: 1

Related Questions