0

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

1 Answer 1

1

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));
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This is exactly what I was looking for, works like a charm inside of Unity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.