Untitled
unknown
csharp
3 years ago
2.6 kB
8
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class Star
{
public double distance { get; set; }
public int bodyCount { get; set; }
public string name { get; set; }
public Vector3 coords { get; set; }
public bool coordsLocked { get; set; }
public Information information { get; set; }
}
public class Information
{
public string allegiance { get; set; }
public string government { get; set; }
public string faction { get; set; }
public string factionState { get; set; }
public int population { get; set; }
public string security { get; set; }
public string economy { get; set; }
}
public class JsonHelper
{
//Usage:
//YouObject[] objects = JsonHelper.getJsonArray<YouObject> (jsonString);
public static T[] getJsonArray<T>(string json)
{
string newJson = "{ \"array\": " + json + "}";
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>> (newJson);
return wrapper.array;
}
//Usage:
//string jsonString = JsonHelper.arrayToJson<YouObject>(objects);
public static string arrayToJson<T>(T[] array)
{
Wrapper<T> wrapper = new Wrapper<T> ();
wrapper.array = array;
return JsonUtility.ToJson (wrapper);
}
[Serializable]
private class Wrapper<T>
{
public T[] array;
}
}
public class GenerateData : MonoBehaviour
{
public GameObject StarPrefab;
// Start is called before the first frame update
async void Start()
{
// Test Star for instantiation
// GameObject star = (GameObject)Instantiate(StarPrefab, new Vector3(0, 0, 0), Quaternion.identity);
// star.name = "Test Star";
// Get star data from EDSM (https://www.edsm.net/en/api-v1)
var url = "https://www.edsm.net/api-v1/sphere-systems?systemName=Colonia&radius=60&showCoordinates=1&showInformation=1";
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url);
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
string strResult = await response.Content.ReadAsStringAsync();
Debug.Log(strResult);
Star[] stars = JsonHelper.getJsonArray<Star>(strResult);
foreach (Star star in stars)
{
Debug.Log(star.information.population);
}
}
}
}
}
Editor is loading...