I am trying to read data from a JSON file using JSONUtility.
One of the fields on the data is a bi dimensional array. I am able to read all the data on the file but the float array. I’ve tried all kinds of data conversion and creating different serializable classes with no success.
Can anybody help me?
This is a sample of the JSON file:
{
"type": "FeatureCollection",
"name": "Tubo_Ramal",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::31983" } },
"features": [
{ "type": "Feature", "properties": { "id": 285004740, "date_insta": "2021\/05\/21", "status": "Ativado", "nominal_di": 32.0, "material": "PE100", "pressure": "Baixa", "actual_len": 3.4, "maintained": null, "service_ta": null, "remarks_id": 0, "manufactur": null, "coating": "Ep�xi", "owner_type": null, "source_id": 0, "source_dat": null, "maop": 0.0, "mop": 0.0, "network_id": null, "asset_id": null, "installed_": "Contratante", "install_me": "Desconhecido", "renewal_re": "Desconhecido", "stopcock_f": null, "curb_box_f": null, "valve_tee_": null, "excess_flo": "N�o Instalado", "near_house": 0.0, "near_hou_1": "Desconhecido", "far_house_": 0.0, "far_hous_1": "Desconhecido", "tap_distan": 0.0, "tap_direct": "Desconhecido", "house_line": null, "cock_dista": 0.0, "cock_direc": "Desconhecido", "cock_refer": "Desconhecido", "tap_at_mai": 0.0, "tap_at_m_1": "Desconhecido", "tap_at_m_2": "Desconhecido", "inter_str_": 0.0, "inter_st_1": "Desconhecido", "inter_st_2": "Desconhecido", "inter_st_3": null, "contract_p": 0.0, "bonded_ind": "N�o", "insulated_": "N�o", "owner_name": null, "model": "P1 - 32MM - 350MBAR", "wall_thick": 0.0, "maop_equip": 0.0, "maop_equ_1": "0000\/00\/00", "maop_syste": "0000\/00\/00", "system_typ": null, "work_order": null, "cg_nom_usu": null, "cg_inserid": "N�o", "cg_materia": null, "cg_unidade": null, "cg_histori": 0, "cg_classif": null }, "geometry": { "type": "LineString", "coordinates": [ [ 324651.169998333440162, 7396407.730006 ], [ 324648.179998333798721, 7396404.350006002001464 ] ] } },
and this is the script I wrote:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class TextLoader : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
TextAsset textFile = (TextAsset)Resources.Load(("Tubo_Ramal"), typeof(TextAsset));
if(textFile != null)
{
string text = textFile.text;
}
feature ft = JsonUtility.FromJson<feature>(textFile.ToString());
Debug.Log(ft.name);
features[] fts = ft.features.ToArray();
List<properties> ppt = new List<properties>();
List<geometry> gt = new List<geometry>();
for(int i = 0; i< fts.Length; i++)
{
string s_properties = JsonUtility.ToJson(fts[i].properties);
properties p = JsonUtility.FromJson<properties>(s_properties);
string s_geometry = JsonUtility.ToJson(fts[i].geometry);
geometry g = JsonUtility.FromJson<geometry>(s_geometry);
gt.Add(g);
ppt.Add(p);
}
for(int i = 0; i<gt.Count; i++)
{
string s_coordinates = JsonUtility.ToJson(gt[i].coordinates);
float[,] a_coordinates = JsonUtility.FromJson<float[,]>(s_coordinates);
Debug.Log(a_coordinates[0,0]);
}
}
}
[System.Serializable]
public class feature
{
public string type;
public string name;
public object crs;
public List<features> features;
}
[System.Serializable]
public class crs
{
public string type;
public object properties;
}
[System.Serializable]
public class features
{
public string type;
public properties properties;
public geometry geometry;
}
[System.Serializable]
public class properties
{
public int id = 285004740;
public string date_insta;
public string status;
public float nominal_di;
public string material;
public string pressure;
public float actual_len;
public string maintained;
public string service_ta;
public int remarks_id;
public string manufactur;
public string coating;
public string owner_type;
public int source_id;
public string source_dat;
public float maop;
public float map;
public int network_id;
public int asset_id;
public string installed_;
public string install_me;
public string renewal_re;
public string stopcock_f;
public string curb_box_f;
public string valve_tee_;
public string excess_flo;
public float near_house;
public string near_hou_1;
public float far_house_;
public string far_hous_1;
public float tap_distan;
public string tap_direct;
public string house_line;
public float cock_dista;
public string cock_direc;
public string cock_refer;
public float tap_at_mai;
public string tap_at_m_1;
public string tap_at_m_2;
public float inter_str_;
public string inter_st_1;
public string inter_st_2;
public string inter_st_3;
public float contract_p;
public string bonded_ind;
public string insulated_;
public string owner_name;
public string model;
public float wall_thick;
public float maop_equip;
public string maop_equ_1;
public string maop_syste;
public string system_typ;
public string work_order;
public string cg_nom_usu;
public string cg_inserid;
public string cg_materia;
public string cg_unidade;
public int cg_histori;
public string cg_classif;
}
[System.Serializable]
public class geometry
{
public string type;
public float[,] coordinates = new float[,]{{Coordenadas.f1,Coordenadas.f2},{Coordenadas.f3,Coordenadas.f4}};
}
[System.Serializable]
public static class Coordenadas
{
public static float f1;
public static float f2;
public static float f3;
public static float f4;
public static float[] a1 = {Coordenadas.f1,Coordenadas.f2};
public static float[] a2 = {Coordenadas.f3,Coordenadas.f4};
}