JSON in mbed

09 Oct 2010

Hi everyone,

I'm looking to read data from a JSON source — I'd like to be able to pull the number of unread notifications from facebook (see here) and then display them on a Nixie tube.

I can pull the data from the internet, and I can display an integer on the nixie tube - but I've jet to figure out how to parse the JSON.

The JSON in question looks something like this:

{
 "messages":{"unread":5,"most_recent":12345678},
 "pokes":{"unread":0,"most_recent":0},
 "shares":{"unread":0,"most_recent":0},
 "friend_requests":{},
 "group_invites":[],
 "event_invites":[]
}
Is there a good (and simple!) way to parse JSON? I've tried the YAJL library already ported to the mbed but I can't figure out how to use it!

18 Oct 2010 . Edited: 18 Oct 2010

Hello, I have been looking for the library for JSON and I've found this.

picojson.h

 

it's really easy to parse json and all you need is only header file.

you can use it like this.

 

#include "mbed.h"
#include "picojson.h"
#include "TextLCD.h"

//TextLCD lcd(p24, p25, p26, p27, p28, p29); // rs, e, d0-d3
TextLCD lcd( p24, p26, p27, p28, p29, p30 ); // rs, e, d0-d3

int main() {

    picojson::value v;
    const char *json = "{\"string\": \"hoge\", \"number\": 3.14}";
    string err = picojson::parse(v, json, json + strlen(json));
        
    string s = v.get("string").get();
    double n = v.get("number").get();
    lcd.locate(0, 0);
    lcd.printf("string:%s, number:%.3f", s.c_str(), n);    
    
}
13 Sep 2011

Compiler does not like v.get("string").get(); Is this really what you meant as I don't see this in picojson.h?

13 Sep 2011

Hi,

This seems to compile (can't test it due to absence of an mBed).

        picojson::value v;
        
        const char *json = "{\"string\": \"hoge\", \"number\": 3.14}";
        
        string err = picojson::parse(v, json, json + strlen(json));
        
        string s = v.get("string").get<string>();
        double n = v.get("number").get<double>();

Interesting link to picojson btw.

wvd_vegt