Go Down

Topic: JSON Library? (Read 33511 times) previous topic - next topic

Roost

As is always the case.

After stepping away for 10 minutes, and rechecking everything, it appears I made the mistake as mentioned

"char* json by char json[]"

Changing that, and all is now working as expected.

Thanks
Bruce

nicoverduin

#16
Aug 25, 2014, 07:42 pm Last Edit: Aug 25, 2014, 07:48 pm by nicoverduin Reason: 1
No doubt it is me  :smiley-mr-green: But, I tried to compile this example under Eclipse using Arduino IDE 1.5.2 and Jantjes plugin and I am getting a bunch of linker errors :
Code: [Select]

Starting combiner
"D:/Arduino/hardware/tools/avr/bin/avr-gcc" -Os -Wl,--gc-sections -mmcu=atmega328p -o "D:/workspace/ArduinoJsonTest/Release/ArduinoJsonTest.elf"    ./ArduinoJsonTest.cpp.o  ./Libraries/ArduinoJson/JsonGenerator/EscapedString.cpp.o ./Libraries/ArduinoJson/JsonGenerator/JsonArrayBase.cpp.o ./Libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp.o ./Libraries/ArduinoJson/JsonGenerator/JsonValue.cpp.o ./Libraries/ArduinoJson/JsonGenerator/Print.cpp.o ./Libraries/ArduinoJson/JsonGenerator/StringBuilder.cpp.o  ./Libraries/ArduinoJson/JsonGenerator.cpp.o   D:/workspace/ArduinoJsonTest/Release/arduino.ar   "D:/workspace/ArduinoJsonTest/Release/arduino.ar" "-LD:/workspace/ArduinoJsonTest/Release" -lm
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonArrayBase::printTo(Print&) const':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonArrayBase.cpp:11: multiple definition of `ArduinoJson::Generator::JsonArrayBase::printTo(Print&) const'
./Libraries/ArduinoJson/JsonGenerator/JsonArrayBase.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonArrayBase.cpp:11: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Internals::StringBuilder::write(unsigned char)':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/StringBuilder.cpp:10: multiple definition of `ArduinoJson::Internals::StringBuilder::write(unsigned char)'
./Libraries/ArduinoJson/JsonGenerator/StringBuilder.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/StringBuilder.cpp:10: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonValue::printPrintableTo(ArduinoJson::Generator::JsonValue::Content const&, Print&)':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonValue.cpp:22: multiple definition of `ArduinoJson::Generator::JsonValue::printPrintableTo(ArduinoJson::Generator::JsonValue::Content const&, Print&)'
./Libraries/ArduinoJson/JsonGenerator/JsonValue.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonValue.cpp:22: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonValue::printBoolTo(ArduinoJson::Generator::JsonValue::Content const&, Print&)':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonValue.cpp:12: multiple definition of `ArduinoJson::Generator::JsonValue::printBoolTo(ArduinoJson::Generator::JsonValue::Content const&, Print&)'
./Libraries/ArduinoJson/JsonGenerator/JsonValue.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonValue.cpp:12: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Internals::EscapedString::printTo(char const*, Print&)':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/EscapedString.cpp:33: multiple definition of `ArduinoJson::Internals::EscapedString::printTo(char const*, Print&)'
./Libraries/ArduinoJson/JsonGenerator/EscapedString.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/EscapedString.cpp:33: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonValue::printStringTo(ArduinoJson::Generator::JsonValue::Content const&, Print&)':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonValue.cpp:30: multiple definition of `ArduinoJson::Generator::JsonValue::printStringTo(ArduinoJson::Generator::JsonValue::Content const&, Print&)'
./Libraries/ArduinoJson/JsonGenerator/JsonValue.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonValue.cpp:30: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonObjectBase::printTo(Print&) const':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:14: multiple definition of `ArduinoJson::Generator::JsonObjectBase::printTo(Print&) const'
./Libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:14: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonValue::printLongTo(ArduinoJson::Generator::JsonValue::Content const&, Print&)':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonValue.cpp:17: multiple definition of `ArduinoJson::Generator::JsonValue::printLongTo(ArduinoJson::Generator::JsonValue::Content const&, Print&)'
./Libraries/ArduinoJson/JsonGenerator/JsonValue.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonValue.cpp:17: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonObjectBase::getMatchingPair(char const*) const':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:42: multiple definition of `ArduinoJson::Generator::JsonObjectBase::getMatchingPair(char const*) const'
./Libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:42: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonObjectBase::remove(char const*)':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:86: multiple definition of `ArduinoJson::Generator::JsonObjectBase::remove(char const*)'
./Libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:86: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonObjectBase::containsKey(char const*) const':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:83: multiple definition of `ArduinoJson::Generator::JsonObjectBase::containsKey(char const*) const'
./Libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:83: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonObjectBase::operator[](char const*)':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:57: multiple definition of `ArduinoJson::Generator::JsonObjectBase::operator[](char const*)'
./Libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:57: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o:(.bss._ZN11ArduinoJson9Generator14JsonObjectBase9nullValueE+0x0): multiple definition of `ArduinoJson::Generator::JsonObjectBase::nullValue'
./Libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:42: first defined here
make: *** [ArduinoJsonTest.elf] Error 1


The example is this one:
Code: [Select]

/*
* Arduino JSON library - Generator example
* Benoit Blanchon 2014 - MIT License
*/

#include "Arduino.h"
#include <JsonGenerator.h>

using namespace ArduinoJson::Generator;

void setup()
{
   Serial.begin(9600);

   JsonArray<2> array;
   array.add<6>(48.756080); // 6 is the number of decimals to print
   array.add<6>(2.302038);  // if not specified, 2 digits are printed

   JsonObject<3> root;
   root["sensor"] = "gps";
   root["time"] = 1351824120;
   root["data"] = array;

   Serial.print(root); // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
}

void loop()
{

}


Any tips where I missed the boat :smiley-mr-green:

@edit: This is interesting. If I compile it direct in the IDE 1.5.2 it works fine....
Met vriendelijke groet / kindest regards
Nico Verduin
www.verelec.nl
Do not PM me for personal consultancy unless you are willing to pay for it.

BenoitB

#17
Sep 01, 2014, 10:05 pm Last Edit: Sep 02, 2014, 09:45 pm by BenoitB Reason: 1
I think I know what's going on here...

To make the library work properly with the Arduino IDE, I added 4 files at the root:

  • JsonGenerator.cpp

  • JsonGenerator.h

  • JsonParser.cpp

  • JsonParser.h



If you open them, you'll see that they only contain #include of the files in the subfolders.
That's a workaround for the Arduino IDE, because it doesn't look for files in the subfolders.

Unfortunately for you, Eclipse does   :P
I suggest you remove the two .cpp files at the root or exclude them from the build.

nicoverduin

Thanks

That solved the problem  :smiley-mr-green:

Nice library  :smiley-mr-green:

Nico
Met vriendelijke groet / kindest regards
Nico Verduin
www.verelec.nl
Do not PM me for personal consultancy unless you are willing to pay for it.

GUNASEELAN

In Arduino how to use both json generator and parser library in a single sketch ?

While i use ,
#include <JsonGenerator.h>
#include <JsonParser.h>

It throws errors stating,

"ArduinoJsonParser\utility\jsmn.cpp.o: In function `jsmn_parse(jsmn_parser*, char const*, jsmntok_t*, unsigned int)':
C:\Users\Guna Qruize\Documents\Arduino\libraries\ArduinoJsonParser\utility/jsmn.cpp:135: multiple definition of `jsmn_parse(jsmn_parser*, char const*, jsmntok_t*, unsigned int)'
ArduinoJson\JsonParser.cpp.o:C:\Users\Guna Qruize\Documents\Arduino\libraries\ArduinoJson\/JsonParser/jsmn.cpp:135: first defined here
ArduinoJsonParser\utility\jsmn.cpp.o: In function `jsmn_init(jsmn_parser*)':
C:\Users\Guna Qruize\Documents\Arduino\libraries\ArduinoJsonParser\utility/jsmn.cpp:250: multiple definition of `jsmn_init(jsmn_parser*)'
ArduinoJson\JsonParser.cpp.o:C:\Users\Guna Qruize\Documents\Arduino\libraries\ArduinoJson\/JsonParser/jsmn.cpp:250: first defined here"

nickgammon

The cross-posting is strong in this one.

http://forum.arduino.cc/index.php?topic=281554.0

@Gunaseelan: When you didn't get a response within 2 minutes, you just decided to post again, is that it? And that was only 30 minutes after your own thread?

Have you not considered that the person who might be able to help is asleep? And might be for another 8 hours?
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics


nickgammon

Maybe we should ban him?
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

nickgammon

Well, it looks like I misquoted.

Quote
The cross-posting is strong in with this one.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

acasado

Benoit et al:

I am trying to use this library, it looks cool and it is well documented.
It works for me on plain JSON strings, however I wonder how to use it on nested objects (curly braces inside curly braces), it parses ok, at least no errors, however I wonder how to get an object inside another objects.
In the below example, I need to get the temp, pressure and humidity.

See this example:

output from:  http://api.openweathermap.org/data/2.5/weather?id=3117735

The ArduinoJson pretty print output is:

{
  "coord": {
    "lon": -3.7,
    "lat": 40.42
  },
  "sys": {
    "message": 0.0401,
    "country": "ES",
    "sunrise": 1420789051,
    "sunset": 1420823192
  },
  "weather": [
    {
      "id": 800,
      "main": "Clear",
      "description": "Sky is Clear",
      "icon": "01d"
    }
  ],
  "base": "cmc stations",
  "main": {
    "temp": 271.744,
    "temp_min": 271.744,
    "temp_max": 271.744,
    "pressure": 980.94,
    "sea_level": 1058.7,
    "grnd_level": 980.94,
    "humidity": 71
  },
  "wind": {
    "speed": 1.51,
    "deg": 20.5025
  },
  "clouds": {
    "all": 0
  },
  "dt": 1420797371,
  "id": 3117735,
  "name": "Madrid",
  "cod": 200
}


and the ArduinoJson iteration of value key pairs is:

coord =>
sys =>
weather =>
base => cmc stations
main =>
wind =>
clouds =>
dt =>
id =>
name => Madrid
cod =>
sys.sunrise =>
sunset =>
pressure =>
humidity =>


Thanks in advance for your help.
AC/.

acasado

Folks,
I found the answer myself and the solution is very cool indeed. It is to create some other objects based on the previous objects, cleaver the implementation indeed.
My congratulations for the great implementation.
See my excerpts of how I solved it.
Cheers.
AC/.

// =========================== Get the JSON objects ============
  JsonObject& root = jsonBuffer.parseObject(json);
 
  if (!root.success()) {
    Serial.println("parseObject() failed");
    delay(10000);
    return;
    }
  Serial.println();
  if (Debug)
    root.prettyPrintTo(Serial);                    // print it on prettyPrint format
  Serial.println();
  JsonObject& sys      = root["sys"];           // create the object sys inside of root
  JsonArray&  weather  = root["weather"]; // weather is an array of objects
  JsonObject& wea      = weather[0];         // we care only for the first object, the main weather station
  JsonObject& main     = root["main"];      // main is the main objct on the weather observations
 
  MADsunrise  = sys["sunrise"];                // get the sunrise time on time_t format
  MADsunset   = sys["sunset"];
  MADdesc     = wea["description"];
  MADbase     = root["name"];
  MADtemp     = main["temp"];
  MADpressure = main["pressure"];
  MADhumidity = main["humidity"];
  MADtemp    -= 273.15;                   // from kelvin to degree celsius
  // =========================== Done ===============================

riadanza

I'm trying to figure out a way to construct a JSON document with nested string value pairs.  The below example you can see "phoneNumber" is a nested array with string value pairs, this is unlike the example where you have a nested array named "data" then just the values without tags.  Is there a way to do this with this library?

Any help is much appreciated.

{
  "firstName": "John",
  "lastName": "Smith",
  "isAlive": true,
  "age": 25,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "office",
      "number": "646 555-4567"
    }
  ],
  "children": [],
  "spouse": null
}

PaulS

Quote
The below example you can see "phoneNumber" is a nested array with string value pairs
Actually, I can't. I can't see phoneNumber anywhere. I do see phoneNumbers.

Seems to me that storing that data in an XML format would be much simpler.
The art of getting good answers lies in asking good questions.

Bertie64

Resurrecting a fairly old thread I know,  but anyway...

I'm using the 'ArduinoJson' library and it works great, but I have a question that I hope someone can help with.

The JSON representation of my nested objects contains a couple of nested arrays.

I can encode and decode the object just fine but I would like to iterate through the object to output the entire structure.

I am using the JsonObject::iterator (recursively for nested objects) and that all works.

The problem I am having is when I encounter one of the nested arrays during the iteration.

I currently have this:

Code: [Select]
void iterate(JsonObject& json) {
for (JsonObject::iterator it=json.begin(); it!=json.end(); ++it) {
if ((it->value).is<JsonObject&>()) {
iterate(it->value);
} else if ((it->value).is<JsonArray&>()) {
JsonArray& myArray = (it->value).asArray();
int count = sizeof(myArray) / sizeof(myArray[0]);
Serial.print("Key : ");
Serial.print(it->key);
Serial.print(" - Values : ");
for (int i = 0; i < count; ++i) {
Serial.print(myArray[i].asString());
if (i != count - 1) {
Serial.print(", ");
} else {
Serial.println();
}
}
} else {
Serial.print("Key : ");
Serial.print(it->key);
Serial.print(" - Value : ");
Serial.println(it->value.asString());
}
}
}


It all does seem to work except that count always seems to equal 1.

Is there something wrong with this:

Code: [Select]
int count = sizeof(myArray) / sizeof(myArray[0]);

The only arrays I am encoding so far have 3 elements and when I use

Code: [Select]
int count = 3;

I works perfectly and prints the three values correctly.

if I print the values returned for sizeof(myArray) and sizeof(myArray[0]) they both return 4.

It's probably me being a bit dim, but myArray seems to be an array with 3 valid elements.
Not sure where I am going wrong here.


PaulS

The JsonArray type is a class that derives from List. List has a size() method. Why not use it?

Code: [Select]
int count = myArray.size();
The art of getting good answers lies in asking good questions.

Go Up