I’m trying to print on a LCD some data which is structured in a JSON file on the Internet.
A browser requesting the URL http://server2.example.com/Users/1234 returns:
{
“Name”: “Foo”,
“Id”: 1234,
“Rank”: 7
}
How could I parse this JSON and display “Rank” field (for example) on a LCD?
I’ve found an Arduino Library called aJson which seems interesting https://github.com/interactive-matter/aJson but I don’t really know if it is Spark compatible or if I need it.
Hi! I haven’t had a chance to play around with parsing JSON strings, so if you have any luck with that library let us know.
Something to watch out for (if you decide to go this route for debugging or otherwise) is that there is currently a bug with using strings in the Spark.variable() function… strings greater than 9 characters seem to have adverse effects. However, this is only if you are making your JSON string available through the cloud API.
@clex I took a look at converting that aJSON for spark core… starts to get involved when it’s looking for Client, Stream and Print classes… also typedef enum and typedef struct don’t seem to work in the web IDE at the moment.
I’m thinking if you know what you want, and you don’t need to absolutely convert the whole JSON object into a structure, you can just buffer your data into a character array or String, and then look for indexOf() the token you are looking for like string.indexOf('Rank"); to get you to the right place… then parse out the bits you need and make conversions on the data (e.g. String to Int, etc…).
This would save a lot of code space… but obviously you have to have specific requirements… like getting the sunset and sunrise unix timestamps from your favorite time keeping website.
I did end up creating a simple way to parse JSON… you’ll have to write very specific code to get at the data you want, but it’s pretty easy:
Note there are some problems with the TCP Client right now, so you’ll have to build locally to get this fully working and include all of the files in the GIST in the right directories.
Work is being done by Dave to fix the issues in the Compiler that prevent us from converting the above mentioned libraries. Maybe this week that second library I posted JSMN could be ported easily.
Bdub, using the Web IDE I was able to compile the simpler JSMN json parser you found. If you or anyone else is interested, I put the library and test code on my github.
These JSON Parsing Libraries are huge and not easy to understand. Can someone maybe paste a code that explains json parsing and variable storing of this simple first example?
kaul, take a look at the JSON parser I posted on another thread. It is based on JSMN which was pointed out by BDub. I suggest you look at the jsmntest.cpp code, function test_simple(). The end of that test function shows the simple JSON test string:
This is essentially the same JSON message structure you wanted but different objects. The JSMN parser will tag each “token” in the JSON message for your to then take action on. I will try and put a specific demo for you when I get a chance.
Kaul, I’m almost done with the demo of the parser for the JSON string you supplied. Did you intend on having an array of one object, made up of 3 objects, each being a string? Or did you want three objects of two INT and one STRING like this:
Kaul, I had the same issue that is why I wrote it the way I did. Once you have terminal open, press any key and it will run once. Pressing a key again and it will run again. I did it this way so setup() or loop() would not hang the wifi waiting for a key. It is nearly impossible to time to the opening of a terminal window with the start of the user sketch!
Just wanted to say I was able to get aJSON working by simply changing the following in aJSON.h:
#include "spark_wiring_stream.h"
#include "spark_wiring_print.h"
#include "spark_wiring_client.h"
//#include <Print.h>
//#include <Stream.h>
//#include <Client.h>
//#include <Arduino.h> // To get access to the Arduino millis() function
I will see about creating a spark-compatible library and adding to the web IDE library list.