Search code examples
c#unity-game-enginelitjson

How can I read the boolean value from litjson


enter image description here

My json file from server is like this:

{"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"}}

How can I read the value "isVertical"?


Solution

  • First of all, your Json is not valid. There should be a ':' between "serviceProps" and {},.

    Here is a fixed verision of your Json data.

    {"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"}}
    

    To answer your question, you can easily extract isVertical by creating a class that represents all the keys then pull the isVertical from that class.

    Unity add Json native support in 5.3 release. The solution below requires that you have 5.3 and above and it should work. Tested with 5.4.0b13 and it works Should work on 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;
    }
    

    Code to Read the isVertical Json value:

    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);
     }