如何从 litjson 中读取布尔值
How can I read the boolean value from litjson
我的 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"?
像这样
// 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"];
首先,您的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"}}
要回答您的问题,您可以通过创建代表所有键的 class 然后从 class 中提取 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);
}
我的 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"?
像这样
// 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"];
首先,您的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"}}
要回答您的问题,您可以通过创建代表所有键的 class 然后从 class 中提取 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);
}