3 回答
TA贡献2003条经验 获得超2个赞
复制粘贴你的 json 和http://json2csharp.com/
你会得到这个:
public class Radial
{
public int radialNumber { get; set; }
public double azimuthDeg { get; set; }
public double elevationDeg { get; set; }
public int duration { get; set; }
public List<double> gateIntensity { get; set; }
}
public class RootObject
{
public int sweepIndex { get; set; }
public int totalRadials { get; set; }
public double beamWidth { get; set; }
public long startingUnixtime { get; set; }
public long endingUnixtime { get; set; }
public int totalGatesPerRay { get; set; }
public double gateDepthMeters { get; set; }
public double distanceToFirstGateMeters { get; set; }
public double meanElevationDeg { get; set; }
public double originLatitude { get; set; }
public double originLongitude { get; set; }
public double originAltitude { get; set; }
public int deviantOriginCount { get; set; }
public List<Radial> radials { get; set; }
}
然后你可以使用 JsonUtility 但你首先需要修改一些内容。将 Serializable 属性添加到每个类并删除属性扩展以生成这些基本变量。
[Serializable]
public class Radial
{
public int radialNumber;
public double azimuthDeg;
public double elevationDeg;
public int duration;
public List<double> gateIntensity;
}
[Serializable]
public class RootObject
{
public int sweepIndex;
// same with all following items
}
RootObject 是您的 json 的顶级类,它在 json 中没有名称,因此会生成默认值。您可以将 RootObject 更改为您想要的任何内容,例如 JsonResponse。现在可以像这样使用了:
void Start()
{
string json = GetJsonFile(); // From download or text file
RootObject ro = JsonUtility.FromJson<RootObject>(json);
print( ro.radials[0].radialNumber);
}
TA贡献1842条经验 获得超21个赞
我在 Unity 中将JsonFx用于我的数据类。
示例类:
using JsonFx.Json;
[Serializable]
[JsonName("MyData")]
public class MyData
{
public int id;
public string name;
public int[] stuff;
}
示例 json:
{
"__Type": "MyData, Assembly-CSharp",
"id": 1,
"name": "new_data",
"stuff" :
[
0,
1,
2
]
}
从 json 创建数据对象:
// Json to MyData object:
// Assume I downloaded a json file
string jsonData = System.Text.Encoding.UTF8.GetString (www.bytes);
jsonData = jsonData.Trim ();
MyData data = MyData.Deserialize(jsonData);
从数据对象创建 json 文件
// MyData object to Json
// Use MyData data object
string file = "mydata.json";
JsonWriterSettings settings = new JsonWriterSettings ();
settings.PrettyPrint = true;
settings.TypeHintName = "__Type";
JsonWriter writer = new JsonWriter (file, settings);
writer.Write (data);
writer.TextWriter.Flush ();
writer.TextWriter.Close ();
- 3 回答
- 0 关注
- 321 浏览
添加回答
举报