interactive-matter / aJson

aJson is an Arduino library to enable JSON processing with Arduino. It easily enables you to decode, create, manipulate and encode JSON directly from and to data structures.
http://interactive-matter.org/2010/08/ajson-handle-json-with-arduino/
567 stars 136 forks source link

Could not get multiple results #54

Open Surapol opened 10 years ago

Surapol commented 10 years ago

With the code below, I could not get next result. Only first query result show up then halt right after the first success function "parseJson" .It does not print the "Circle" after the function.

include

char jsonString[256];

void setup() { String js=""; Serial.begin(115200); Serial.println(jsonString); Serial.println("Starting to parse"); js+="{"; js+= "\"query\":{\"count\":1,\"created\":\"2012-08-04T14:46:03Z\",\"lang\":\"en-US\",\"results\":{\"item\":{\"title\":\"Handling FTP usernames with @ in them\"}}},"; js+= "\"circle0\":{\"x\":5,\"y\":10,\"r\":15},"; js+= "\"string0\":{\"x\":20,\"y\":25}"; js+="}"; js.toCharArray(jsonString,sizeof(jsonString)); //Serial.println("Circle"); //parseCircle(jsonString); //Serial.println("String"); //parseString(jsonString); parseJson(jsonString); Serial.println("Circle"); } boolean parseJson(char jsonString){ aJsonObject *root=aJson.parse(jsonString); if(root!=NULL){ aJsonObject query = aJson.getObjectItem(root, "query"); if (query != NULL) { //Serial.println("Parsed successfully 2 " ); aJsonObject* results = aJson.getObjectItem(query, "results");

  if (results != NULL) {
    //Serial.println("Parsed successfully 3 " );
    aJsonObject* item = aJson.getObjectItem(results, "item"); 

    if (item != NULL) {
      //Serial.println("Parsed successfully 4 " );
      aJsonObject* title = aJson.getObjectItem(item, "title"); 

      if (title != NULL) {
        //Serial.println("Parsed successfully 5 " );
        //value = title->valuestring;
        Serial.println(title->valuestring);
        Serial.println("After print?");
        aJson.deleteItem(title);
      }
      aJson.deleteItem(item);
    }
    aJson.deleteItem(results);
  }
  aJson.deleteItem(query);
}
aJson.deleteItem(root);    

} Serial.println("Out"); return true; } boolean parseCircle(char jsonString) { aJsonObject root = aJson.parse(jsonString); if (root != NULL) {
aJsonObject _circle=aJson.getObjectItem(root,"circle0"); if(circle!=NULL){ aJsonObject x=aJson.getObjectItem(circle,"x"); if(x!=NULL){ if(x->type==aJson_Int){ Serial.print("Circle x:"); Serial.println(x->valueint); } aJson.deleteItem(x); } else{ Serial.print("Circle x null"); } aJson.deleteItem(circle); } else{ Serial.println("Circle Null"); } //aJson.deleteItem(root); } else{ Serial.println("Circle root null"); } } boolean parseString(char jsonString) { aJsonObject_ root = aJson.parse(jsonString); if (root != NULL) {
aJsonObject string=aJson.getObjectItem(root,"string0"); if(string!=NULL){ aJsonObject x=aJson.getObjectItem(string,"x"); if(x!=NULL){ if(x->type==aJson_Int){ Serial.print("string0 x:"); Serial.println(x->valueint); } aJson.deleteItem(x); } else{ Serial.print("String x null"); } aJson.deleteItem(string); } else{ Serial.println("String Null"); } //aJson.deleteItem(root); } else{ Serial.println("String root null"); } }

void loop() { // Nothing to do ; }