来自服务器的我的json文件是这样的:
{"status":"success","data":{"projectId":"572ca0cde163d","sensorId":"572ca2deea163b","createTime":1462514044,"updateTimeStamp":1462514044,"recognPicUrl":"http://192.168.1.115:8500/dddd.jpg","drawingModule":{"subjectTemplateId":"16","drawingUnits":[{"drawingUnitId":"572ca0c4f14c023cdeea163c",
"drawingResources":
[{"resourceUrl":"http://192.168.1.115:8300/dds.png","serviceProps":{},"resourceType":"IMG"}],"drawingComponentId":"1"}],
"templateConfig":{"isVertical":false}},"targetId":"ba0a0d83c657e49eb312"}}
如何读取值“isVertical”?
答案 0 :(得分:1)
首先,您的Json
不有效。应该有一个' :
'在"serviceProps"
和{},
之间。
以下是Json数据的固定版本。
{"status":"success","data":{"projectId":"572ca0cde163d","sensorId":"572ca2deea163b","createTime":1462514044,"updateTimeStamp":1462514044,"recognPicUrl":"http://192.168.1.115:8500/dddd.jpg","drawingModule":{"subjectTemplateId":"16","drawingUnits":[{"drawingUnitId":"572ca0c4f14c023cdeea163c",
"drawingResources":
[{"resourceUrl":"http://192.168.1.115:8300/dds.png","serviceProps":{},"resourceType":"IMG"}],"drawingComponentId":"1"}],
"templateConfig":{"isVertical":false}},"targetId":"ba0a0d83c657e49eb312"}}
要回答您的问题,您可以通过创建代表所有密钥的类轻松提取isVertical
,然后从该类中提取isVertical
。
Unity在 5.3 版本中添加Json
原生支持。下面的解决方案要求你有5.3及以上,它应该工作。使用 5.4.0b13 进行测试,它的工作原理应该适用于 5.3 。
[System.Serializable]
public class ServiceProps
{
}
[System.Serializable]
public class DrawingResource
{
public string resourceUrl;
public ServiceProps serviceProps;
public string resourceType;
}
[System.Serializable]
public class DrawingUnit
{
public string drawingUnitId;
public List<DrawingResource> drawingResources;
public string drawingComponentId;
}
[System.Serializable]
public class TemplateConfig
{
public bool isVertical;
}
[System.Serializable]
public class DrawingModule
{
public string subjectTemplateId;
public List<DrawingUnit> drawingUnits;
public TemplateConfig templateConfig;
}
[System.Serializable]
public class Data
{
public string projectId;
public string sensorId;
public int createTime;
public int updateTimeStamp;
public string recognPicUrl;
public DrawingModule drawingModule;
public string targetId;
}
[System.Serializable]
public class PlayerInfo
{
public string status;
public Data data;
}
读取isVertical
Json值的代码:
void test()
{
string messageFromServer = "";
messageFromServer = "{\"status\":\"success\",\"data\":{\"projectId\":\"572ca0cde163d\",\"sensorId\":\"572ca2deea163b\",\"createTime\":1462514044,\"updateTimeStamp\":1462514044,\"recognPicUrl\":\"http://192.168.1.115:8500/dddd.jpg\",\"drawingModule\":{\"subjectTemplateId\":\"16\",\"drawingUnits\":[{\"drawingUnitId\":\"572ca0c4f14c023cdeea163c\",\r\n \"drawingResources\":\r\n [{\"resourceUrl\":\"http://192.168.1.115:8300/dds.png\",\"serviceProps\":{},\"resourceType\":\"IMG\"}],\"drawingComponentId\":\"1\"}],\r\n \"templateConfig\":{\"isVertical\":false}},\"targetId\":\"ba0a0d83c657e49eb312\"}}";
PlayerInfo playerInfo;
playerInfo = new PlayerInfo();
playerInfo.data = new Data();
playerInfo.data.drawingModule = new DrawingModule();
playerInfo.data.drawingModule.drawingUnits = new List<DrawingUnit>();
for (int i = 0; i < playerInfo.data.drawingModule.drawingUnits.Count; i++)
{
playerInfo.data.drawingModule.drawingUnits[i].drawingResources = new List<DrawingResource>();
}
playerInfo.data.drawingModule.templateConfig = new TemplateConfig();
playerInfo = JsonUtility.FromJson<PlayerInfo>(messageFromServer);
Debug.Log("Status: " + playerInfo.status);
Debug.Log("Vertical: " + playerInfo.data.drawingModule.templateConfig.isVertical);
}
答案 1 :(得分:0)
喜欢这个
// read your json data into a variable
var jsonData = {"status":"success","data":{"projectId":"572ca0cde163d","sensorId":"572ca2deea163b","createTime":1462514044,"updateTimeStamp":1462514044,"recognPicUrl":"http://192.168.1.115:8500/dddd.jpg","drawingModule":{"subjectTemplateId":"16","drawingUnits":[{"drawingUnitId":"572ca0c4f14c023cdeea163c",
"drawingResources":[{"resourceUrl":"http://192.168.1.115:8300/dds.png","serviceProps"{},"resourceType":"IMG"}],"drawingComponentId":"1"}],
"templateConfig":{"isVertical":false}},"targetId":"ba0a0d83c657e49eb312"}};
//then read it like this
var isVertical = jsonData.templateConfig.isVertical
修改强>
这应该是你想要它的方式
bool isVertical = (bool)jsondata["drawingModule"]["templateConfig"]["isVertical"];