2

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"?

Mark Chackerian
  • 21,866
  • 6
  • 108
  • 99
Mark2
  • 49
  • 7

2 Answers2

1

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);
 }
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thank you !,it works in unity5.3.4. but for some complex reason we have to use unity5.2 version in our project. can you help me change it in Litjson way ? thanks for your reply. – Mark2 May 08 '16 at 00:52
  • 1
    @QibiaoTu I don't use Litjson. Why can't you use 5.3.4? Try replacing `playerInfo = JsonUtility.FromJson(messageFromServer);` with `playerInfo = JsonMapper.ToObject(messageFromServer);`. It might work. Not sure since I hate external libraries. Let me know if that works. – Programmer May 08 '16 at 01:17
0

Like this

// 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

Edit

It should be this in the way you want it

bool isVertical = (bool)jsondata["drawingModule"]["templateConfig"]["isVertical"];
Pawan Nogariya
  • 8,330
  • 12
  • 52
  • 105
  • thanks for your reply.but I still can't get the value. i can get the "subjectTemplateId" like this:JsonData jsondata = JsonMapper.ToObject(configuration_json); string TemID = (string)jsondata["data"]["drawingModule"]["subjectTemplateId"]; in the same way I can't get the "isVertical" value,like this: JsonData jsondata = JsonMapper.ToObject(configuration_json); bool isVertical = (bool)jsondata["data"]["drawingModule"]["templateConfig"]["isVertical"]; – Mark2 May 07 '16 at 13:22
  • One moment, your json is not proper, I just check it here http://jsonlint.com/, it has errors – Pawan Nogariya May 07 '16 at 13:33
  • Your `"serviceProps" {},` doesn't have colon. Is it a typo or this is the exact data coming from server? – Pawan Nogariya May 07 '16 at 13:35
  • Thanks , I use bool isVertical = (bool)jsondata["templateConfig"]["isVertical"];but it still not work. – Mark2 May 07 '16 at 13:46
  • Sorry, I cannot explain you anymore I already explained you that you have problem with this part of your json `"serviceProps" {},` – Pawan Nogariya May 07 '16 at 14:23