如何解析模块值的JSON结果并采取措施?

时间:2015-05-14 21:18:06

标签: c++ json arduino arduino-yun

如何解析URL输出(JSON包),如下所示:

{"result":"ok","db":[{"id":"2","createdate":"0000-00-00 00:00:00","modifydate":"0000-00-00 00:00:00","module":null,"serial":null,"company":"1","firstname":"An ","lastname":"","email1":"","email2":"","email3":"","phone":null,"mobile":null,"pincode":null,"username":null,"email4":""},{"id":"3","createdate":"0000-00-00 00:00:00","modifydate":"0000-00-00 00:00:00","module":"red","serial":null,"company":"2","firstname":"An ","lastname":"","email1":"","email2":"","email3":"","phone":null,"mobile":null,"pincode":null,"username":null,"email4":""},{"id":"4","createdate":"0000-00-00 00:00:00","modifydate":"0000-00-00 00:00:00","module":"red","serial":null,"company":"3","firstname":"An ","lastname":"","email1":"","email2":"","email3":"","phone":null,"mobile":null,"pincode":null,"username":null,"email4":""}

从URL结果中仅读取id和模块值

module:null and id:2 
module:red and id:3  
module:red and id:4

如果值为"红色"然后执行digitalWrite(id_number, HIGH);(在这种情况下,两个执行应该发生在引脚3和引脚4)

Arduino YUN代码:

#include <Bridge.h>
#include <HttpClient.h>

void setup() {
  pinMode(13, OUTPUT);
  Bridge.begin();
}

void loop() {      
  HttpClient client;
  /* This returns JSON result */
  client.get("http://example.com/ajax/pinoutput");
  String result;

  while (client.available()) {
    char c = client.read();
    result = result + c;
  }

  /* Check for the value RED from JSON result and read the ID which is to insert in digitalWrite(HERE); */
  if(result.indexOf("red") < 0) {
    digitalWrite(13, HIGH);
    delay(1000);
  } else {
    digitalWrite(13, LOW);    
    delay(1000);
  }

  /* Delay the crawl interval */
  delay(2000);
}

1 个答案:

答案 0 :(得分:0)

你需要一个解析器。完成这项工作的简单库如下:

http://sourceforge.net/projects/cjson/