Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of array #75

Open
sticilface opened this issue Sep 27, 2015 · 3 comments
Open

Use of array #75

sticilface opened this issue Sep 27, 2015 · 3 comments

Comments

@sticilface
Copy link

I have the following Json string

{
  "bri" : 14,
  "xy" : [
    0.2404497,
    0.5637349
  ],
  "on" : true
}

I can retrieve the bri values using

    if (aJsonObject* briState = aJson.getObjectItem(parsedRoot, "bri")) {
      bri = briState->valueint;
      //bri /= 255; 
    }

but i can't seem to work out how to get the parts 1 and 2 of the "xy" into a struct containing x and y as floats. could you point me in the right direction?

@nw2s
Copy link

nw2s commented Sep 27, 2015

Any way to do this that's not in an issue? Maybe not. If you need some sample parsing code, check out getIntCollectionFromJSON

at https://github.com/nw2s/b/blob/b-1.1.0-unstable/sketches/libraries/nw2s/JSONUtil.cpp

Hint: You need to know that element is an array and then treat it as such. Collections are different from simple values. They have values for methods that normally wouldn't make sense, namely getArraySize and getArrayItem.

-s

@sticilface
Copy link
Author

I did look but nothing jumped out.
So I've managed to get it to parse the float as a double, using this:

    if (aJsonObject* xyState =  aJson.getObjectItem(parsedRoot, "xy")) {

      //aJsonObject* xyColourSize = aJson.getArraySize(xyState);
      aJsonObject* xyColourSpaceX = aJson.getArrayItem(xyState, 0);
      aJsonObject* xyColourSpaceY = aJson.getArrayItem(xyState, 1);


  if (xyColourSpaceX != NULL && xyColourSpaceX != NULL) {

      //xyColourSpace[0] = xyState->json_string;
      //xyColourSpace.y = xyState->aJson_Array;

      Serial.print("ARRAY RECIEVED SIZE = ");
      Serial.println(aJson.getArraySize(xyState));
       Serial.print("X = ");
       Serial.println(xyColourSpaceX->valuefloat);
}
}

However, as you can see from the first post there 7 digits after the decimal, and I could really do with using all of them not just 2. I thought I could parse as a string and then convert it to float, but parsing as string does not work.

any idea?

@nw2s
Copy link

nw2s commented Sep 29, 2015

So you're saying that serial.println is only printing two decimal places? Before blaming the framework parsing, be sure that it's not the println formatting that's truncating the float. I can't see any more of the code that might or might not be contributing, so that's all I could guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants