gpt4 book ai didi

c# - 如何从 litjson 中读取 bool 值

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:36 26 4
gpt4 key购买 nike

enter image description here

我从服务器传来的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”?

最佳答案

首先,您的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);
}

关于c# - 如何从 litjson 中读取 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37089000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com