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

JSON string not parsing properly #55

Open afuhrtrumpet opened 10 years ago

afuhrtrumpet commented 10 years ago

Hi, I have been trying to parse this string using aJson

{ "songs": [ { "name": "Song Of Time", "notes": [69, 62, 65, 69, 62, 65, 69, 72, 71, 67, 65, 67, 69, 62, 60, 64, 62], "times": [600, 900, 600, 600, 900, 600, 300, 300, 600, 600, 300, 300, 600, 600, 300, 300, 900], "nunchuck": [3, 1, 2, 3, 1, 2] } ] }

using this code

aJsonObject* root = aJson.parse(songText);
  aJsonObject* songData = aJson.getObjectItem(root, "songs");
  aJsonObject* song = aJson.getArrayItem(songData, 0);
  aJsonObject* songName = aJson.getObjectItem(song, "name");
  songs[0].name = songName->valuestring;
  aJsonObject* noteData = aJson.getObjectItem(song, "notes");
  aJsonObject* timeData = aJson.getObjectItem(song, "times"); 
  int numberNotes = aJson.getArraySize(noteData);
  for (int i = 0; i < numberNotes; i++) {
    aJsonObject* note = aJson.getArrayItem(noteData, i);
    aJsonObject* time = aJson.getArrayItem(timeData, i);
    songs[0].notes.push_back(note->valueint);
    songs[0].notes.push_back(time->valueint);
  }
  aJsonObject* nunchuckData = aJson.getObjectItem(song, "nunchuck");
  int numberNunchuck = aJson.getArraySize(nunchuckData);
  for (int i = 0; i < numberNunchuck; i++) {
    aJsonObject* note = aJson.getArrayItem(nunchuckData, i);
    songs[0].nunchuck_notes.push_back(note->valueint);
  }

And have been getting an empty string for the name, and usually identical random numbers for the other two. I am not sure what the issue is here, but I don't think it is memory as I had 651 bytes of memory remaining when I checked. Could it be the extra whitespace in the string?