0

三天我徒劳地尝试解析一个 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”];
4

1 回答 1

1

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

为什么不可能?

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

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

a)获取字符串并在您自己的代码中解析它或

b) 获取该字符串,使其成为有效的 JSON,例如通过类似的方式"{ \"arr\": " + arrayString + "}",然后将该新字符串再次解析为 JSON。

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

于 2019-02-04T13:01:51.513 回答