JSON 역 직렬화

앤디

예를 들어 config에서 반환되는 다른 필드로 인해 아래 JSON 스 니펫을 deserialzer하는 데 어려움을 겪고 있습니다. 각 구성 블록에 있기 때문에 config.on에서 정상적으로 작동하지만 다른 것들은 모두 다르기 때문에 어떻게해야합니까? 이 주위에? JSON은 서로 다른 필드로 연결된 모든 다른 센서를 반환합니다.

dynamic obj = JsonConvert.DeserializeObject(response);
//
foreach (var item in obj)
{
    string temperature = item.ToString();         //this shows the full object in text
    //    Debug.WriteLine(temperature);           //this shows the full object in text

    dynamic group = item;

    string idstring = group.Name.ToString();

    foreach (var prop in group)
    {
        string name = prop.name; 
        string temp = prop.config.on;
        //string temp = prop.state.lastupdated;
        //string temp = prop.swversion;

        //Debug.WriteLine(temp);

        string namestring = name.ToString();
        string tempstring = temp.ToString();

        arr[0] = idstring.ToLower();
        arr[1] = namestring.ToLower();
        arr[2] = tempstring.ToLower();

JSON 응답의 일부

{
    "1": {
        "state": {
            "daylight": true,
            "lastupdated": "2017-03-10T07:01:00"
        },
        "config": {
            "on": true,
            "configured": true,
            "sunriseoffset": 30,
            "sunsetoffset": -30
        },
        "name": "Daylight",
        "type": "Daylight",
        "modelid": "PHDL00",
        "manufacturername": "Philips",
        "swversion": "1.0"
    },
    "2": {
        "state": {
            "temperature": 1830,
            "lastupdated": "2017-03-10T08:11:51"
        },
        "config": {
            "on": true,
            "battery": 100,
            "reachable": true,
            "alert": "none",
            "ledindication": false,
            "usertest": false,
            "pending": [

},
"name": "Hall",
"type": "ZLLPresence",
"modelid": "SML001",
"manufacturername": "Philips",
"swversion": "6.1.0.18912",
"uniqueid": "00:17:88:01:02:01:8b:be-02-0406"
  },
 "4": {
    "state": {
  "lightlevel": 12270,
  "dark": true,
  "daylight": false,
  "lastupdated": "2017-03-10T08:14:28"
},
"config": {
  "on": true,
  "battery": 100,
  "reachable": true,
  "alert": "none",
  "tholddark": 16011,
  "tholdoffset": 7000,
  "ledindication": false,
  "usertest": false,
  "pending": [

  ]
},
"name": "Hue ambient light sensor 1",
"type": "ZLLLightLevel",
"modelid": "SML001",
"manufacturername": "Philips",
"swversion": "6.1.0.18912",
"uniqueid": "00:17:88:01:02:01:8b:be-02-0400"
  },
  "5": {
"state": {
  "temperature": 1919,
  "lastupdated": "2017-03-10T08:12:50"
Renats Stozkovs

누락 된 속성을 String여러 번 변환하기 위해 추가로 창의적 일 필요가 없습니다 . 따라서 이렇게하는 대신 :

string namestring = name.ToString();
string tempstring = temp.ToString();

null상태 를 미리 확인하고 (필요한 경우) 그에 따라 조치를 취하기 만하면 됩니다. Json.NET은 속성이 누락 된 경우 예외를 throw하지 않으므로 필요한 모든 필드를 가져온 다음 그에 따라 비즈니스 논리를 실행할 수 있습니다.

string name = prop.name;
string temp = prop.config.on;
string battery = prop.config.battery;
if (battery == null) 
    // maybe throw exception, maybe assign default value; whatever fits you

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사