ArduinoJson,使用方括号前面的引号解析数组时出错

时间:2019-02-04 11:54:51

标签: json parsing arduino home-automation arduino-c++

我花了三天时间试图解析JSON字符串(流)。 今天,我发现无法解析方括号前面带有引号的JSON字符串。 不幸的是,我无法以其他方式查询JSON字符串。

您是否知道如何获取数据? 附加了完整的JSON字符串和我的代码。

我必须拥有所有数据,但首先要关注数组“ javascript.0.Wetterstation.HistTemp1Date”和“ javascript.0.Wetterstation.HistTemp1”。 我完全绝望了,无法继续。

JSON输入:

{
  "javascript.0.Wetterstation.TempDraussenMin": {
    "val": -0.1,
    "ack": false,
    "ts": 1549216440089,
    "q": 0,
    "from": "system.adapter.javascript.0",
    "lc": 1549216440080
  },
  "javascript.0.Wetterstation.TempDraussenMax": {
    "val": 5.5,
    "ack": false,
    "ts": 1549198440031,
    "q": 0,
    "from": "system.adapter.javascript.0",
    "lc": 1549198440031
  },
  "javascript.0.Wetterstation.TempDraussenMinTime": {
    "val": "18:54:00 - 03.02.2019",
    "ack": false,
    "ts": 1549216440096,
    "q": 0,
    "from": "system.adapter.javascript.0",
    "lc": 1549216440096
  },
  "javascript.0.Wetterstation.TempDraussenMaxTime": {
    "val": "13:54:00 - 03.02.2019",
    "ack": false,
    "ts": 1549198440034,
    "q": 0,
    "from": "system.adapter.javascript.0",
    "lc": 1549198440034
  },
  "javascript.0.Wetterstation.LastResetMinMax": {
    "val": "11:45:40 - 03.02.2019",
    "ack": false,
    "ts": 1549190740415,
    "q": 0,
    "from": "system.adapter.javascript.0",
    "lc": 1549190740415
  },
  "javascript.0.Wetterstation.UpdateAnAus": {
    "val": false,
    "ack": false,
    "ts": 1548212875725,
    "q": 0,
    "from": "system.adapter.admin.0",
    "lc": 1548212875725
  },
  "javascript.0.Wetterstation.ResetMinMax": {
    "val": true,
    "ack": false,
    "ts": 1549190740396,
    "q": 0,
    "from": "system.adapter.web.0",
    "lc": 1547985029727
  },
  "javascript.0.Wetterstation.HistTemp1Date": {
    "val": "[\"2019-1-31 20:00:00\",\"2019-1-31 21:00:00\",\"2019-1-31 22:00:00\",\"2019-1-31 23:00:00\",\"2019-2-1 00:00:00\",\"2019-2-1 01:00:00\",\"2019-2-1 02:00:00\",\"2019-2-1 03:00:00\",\"2019-2-1 04:00:00\",\"2019-2-1 05:00:00\",\"2019-2-1 06:00:00\",\"2019-2-1 07:00:00\",\"2019-2-1 08:00:00\",\"2019-2-1 09:00:00\",\"2019-2-1 10:00:00\",\"2019-2-1 11:00:00\",\"2019-2-1 12:00:00\",\"2019-2-1 13:00:00\",\"2019-2-1 14:00:00\",\"2019-2-1 15:00:00\",\"2019-2-1 16:00:00\",\"2019-2-1 17:00:00\",\"2019-2-1 18:00:00\",\"2019-2-1 20:00:00\",\"2019-2-1 21:00:00\",\"2019-2-1 22:00:00\",\"2019-2-1 23:00:00\",\"2019-2-2 00:00:00\",\"2019-2-2 01:00:00\",\"2019-2-2 02:00:00\",\"2019-2-2 03:00:00\",\"2019-2-2 04:00:00\",\"2019-2-2 05:00:00\",\"2019-2-2 06:00:00\",\"2019-2-2 07:00:00\",\"2019-2-2 08:00:00\",\"2019-2-2 09:00:00\",\"2019-2-2 10:00:00\",\"2019-2-2 11:00:00\",\"2019-2-2 12:00:00\",\"2019-2-2 13:00:00\",\"2019-2-2 14:00:00\",\"2019-2-2 15:00:00\",\"2019-2-2 16:00:00\",\"2019-2-2 17:00:00\",\"2019-2-2 18:00:00\",\"2019-2-2 19:00:00\",\"2019-2-2 20:00:00\",\"2019-2-2 21:00:00\",\"2019-2-2 22:00:00\",\"2019-2-2 23:00:00\",\"2019-2-3 00:00:00\",\"2019-2-3 01:00:00\",\"2019-2-3 02:00:00\",\"2019-2-3 03:00:00\",\"2019-2-3 04:00:00\",\"2019-2-3 05:00:00\",\"2019-2-3 06:00:00\",\"2019-2-3 07:00:00\",\"2019-2-3 08:00:00\",\"2019-2-3 09:00:00\",\"2019-2-3 10:00:00\",\"2019-2-3 11:00:00\",\"2019-2-3 12:00:00\",\"2019-2-3 13:00:00\",\"2019-2-3 14:00:00\",\"2019-2-3 15:00:00\",\"2019-2-3 16:00:00\",\"2019-2-3 17:00:00\",\"2019-2-3 18:00:00\"]",
    "ack": false,
    "ts": 1549217316165,
    "q": 0,
    "from": "system.adapter.javascript.0",
    "lc": 1549217316165
  },
  "javascript.0.Wetterstation.HistTemp1": {
    "val": "[-1.24,-0.67,-0.13,-0.58,-1.26,-1.81,-1.49,-0.75,-0.52,-0.4,-0.04,0,-0.13,-0.06,0.37,0.87,1.51,1.78,3.05,3.7,3.7,3.06,2.48,2.09,1.9,0.93,-0.28,-0.64,-0.49,-0.36,-0.31,-0.06,0.15,0.42,0.65,0.58,0.39,0.62,1.09,1.31,0.83,0.71,1.04,0.94,0.5,0.59,0.54,0.5,0.56,0.4,-0.34,-0.13,0.11,0.2,-0.02,-0.37,0.03,-0.21,-0.53,-1.05,-0.86,-0.49,0.36,1.77,3.86,4.96,5.13,5.21,4.14,1.48]",
    "ack": false,
    "ts": 1549217316168,
    "q": 0,
    "from": "system.adapter.javascript.0",
    "lc": 1549217288962
  }
}

代码:

if (client.connect(host, 8088))

  {
    Serial.println("connected]");

    Serial.println("[Sending a request]");
    client.print(String("GET /getBulk/javascript.0.Wetterstation.*") + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" +
                 "Connection: close\r\n" +
                 "\r\n"
                );



    char status[32] = {0};
    client.readBytesUntil('\r', status, sizeof(status));
    if (strcmp(status, "HTTP/1.1 200 OK") != 0) {
      Serial.print(F("Unexpected response: "));
      Serial.println(status);
       return;
    }
    char endOfHeaders[] = "\r\n\r\n";
    if (!client.find(endOfHeaders)) {
      Serial.println(F("Invalid response"));
      return;
    }

    Serial.println("[Response:]");
    while (client.connected() || client.available())
    {
      if (client.available())
      {
        String line = client.readStringUntil('\n');
        Serial.println(line);
      }
    }


    client.stop();
    Serial.println("\n[Disconnected]");
  }
  else
  {
    Serial.println("connection failed!]");
    client.stop();
  }

  Serial.println(client);

  const int capacity = 4081;
  DynamicJsonBuffer jsonBuffer(capacity);

  JsonObject&  parsed = JSONBuffer.parseObject(client);
  JsonArray& values = parsed[javascript.0.Wetterstation.HistTemp1][“val”];

1 个答案:

答案 0 :(得分:1)

  

无法解析在方括号前面带有引号的JSON字符串

为什么不可能呢?

"val": "[-1.24,-0.67,...]"是一个名为val的字段,被分配一个 string 。您应该能够简单地以字符串形式访问其值。这就是JSON所提供的。

如果您想进一步处理该字符串,则有两个选择:

a)提取字符串并以您自己的代码进行解析,或者

b)接受该字符串,使其成为有效的JSON,例如"{ \"arr\": " + arrayString + "}"之类的东西,然后再次将该新字符串解析为JSON。

最好的方法当然是让源头首先提供正确的JSON。