views:

255

answers:

6
+2  Q: 

C++ JSON parser

Dear reader,

I'm working on a twitter client which uses the twitter streaming json api. Twitter advices JSON as XML version is deprecated. I'm looking for a good JSON parser which can parse the json data below.

I'm receiving this JSON which I want to be able to read/parse using a JSON parser.

{
"in_reply_to_status_id": null,
"text": "Home-plate umpire Crawford gets stung http://tinyurl.com/27ujc86",
"favorited": false,
"coordinates": null,
"in_reply_to_user_id": null,
"source": "<a href=\"http://apiwiki.twitter.com/\" rel=\"nofollow\">API</a>",
"geo": null,
"created_at": "Fri Jun 18 15:12:06 +0000 2010",
"place": null,
"user": {
    "profile_text_color": "333333",
    "screen_name": "HostingViral",
    "time_zone": "Pacific Time (US & Canada)",
    "url": "http://bit.ly/1Way7P",
    "profile_link_color": "228235",
    "profile_background_image_url": "http://s.twimg.com/a/1276654401/images/themes/theme14/bg.gif",
    "description": "Full time Internet Marketer - Helping other reach their Goals\r\nhttp://wavemarker.com",
    "statuses_count": 1944,
    "profile_sidebar_fill_color": "c7b7c7",
    "profile_background_tile": true,
    "contributors_enabled": false,
    "lang": "en",
    "notifications": null,
    "created_at": "Wed Dec 30 07:50:52 +0000 2009",
    "profile_sidebar_border_color": "120412",
    "following": null,
    "geo_enabled": false,
    "followers_count": 2485,
    "protected": false,
    "friends_count": 2495,
    "location": "Working at Home",
    "name": "Johnathan Thomas",
    "verified": false,
    "profile_background_color": "131516",
    "profile_image_url": "http://a1.twimg.com/profile_images/600114776/nessykalvo421_normal.jpg",
    "id": 100439873,
    "utc_offset": -28800,
    "favourites_count": 0
}, "in_reply_to_screen_name": null,
"id": 16477056501,
"contributors": null,
"truncated": false
}

*This is the raw string (above it beautified) *

{"in_reply_to_status_id":null,"text":"Home-plate umpire Crawford gets stung http://tinyurl.com/27ujc86","favorited":false,"coordinates":null,"in_reply_to_user_id":null,"source":"&lt;a href=\"http://apiwiki.twitter.com/\" rel=\"nofollow\">API</a>","geo":null,"created_at":"Fri Jun 18 15:12:06 +0000 2010","place":null,"user":{"profile_text_color":"333333","screen_name":"HostingViral","time_zone":"Pacific Time (US & Canada)","url":"http://bit.ly/1Way7P","profile_link_color":"228235","profile_background_image_url":"http://s.twimg.com/a/1276654401/images/themes/theme14/bg.gif","description":"Full time Internet Marketer - Helping other reach their Goals\r\nhttp://wavemarker.com","statuses_count":1944,"profile_sidebar_fill_color":"c7b7c7","profile_background_tile":true,"contributors_enabled":false,"lang":"en","notifications":null,"created_at":"Wed Dec 30 07:50:52 +0000 2009","profile_sidebar_border_color":"120412","following":null,"geo_enabled":false,"followers_count":2485,"protected":false,"friends_count":2495,"location":"Working at Home","name":"Johnathan Thomas","verified":false,"profile_background_color":"131516","profile_image_url":"http://a1.twimg.com/profile_images/600114776/nessykalvo421_normal.jpg","id":100439873,"utc_offset":-28800,"favourites_count":0},"in_reply_to_screen_name":null,"id":16477056501,"contributors":null,"truncated":false}

I've tried multiple JSON parsers from json.org though I've tried 4 now and can't find one which can parse above json.

Kind regards, Pollux

A: 

I'm a real big fan of jsoncpp.

mos
Tried it, didn't work
pollux
+2  A: 

Python 2.6 seems to parse your example fine. However, I did have problems when I initially tried. Python puked as well if I pasted the string directly without escaping the backslashes. Possibly that is related to your issue. Make sure the backslashes aren't being processed along the way. Or try double escaping them.


Update: I tried it with JsonCpp 0.5.0, and it worked perfectly fine. I copied your raw string into a file name example_packed.json. It outputs a sorted and pretty printed version.

#include <fstream>
#include <iostream>

#include "json/json.h"

int main()
{
   Json::Value root;
   std::ifstream file("example_packed.json");
   file >> root;
   std::cout << root;
}

I suspect your issue really is just a copy-paste error. If you copy-paste the raw string directly into a c++ string literal the escapes get processed. You'll need to double escape them when in string form. Otherwise, just read it from a file to avoid the confusion.

caspin
Ah interesting, I didn't thought about the backslahes when I checked the file.
Matthieu M.
A: 

JSON Spirit doesn't have any problem with a properly escaped input string. Reduced example:

const std::string test =
"{"
"    \"text\": \"Home-plate umpire Crawford gets stung http://tinyurl.com/27ujc86\","
"    \"favorited\": false,"
"    \"source\": \"<a href=\\\"http://apiwiki.twitter.com/\\\" rel=\\\"nofollow\\\">API</a>\","
"    \"user\": {"
"        \"name\": \"Johnathan Thomas\""
"    }"
"}";

int main() {
    namespace js = json_spirit;
    js::mValue top;
    js::read_string(std::string(test), top);
    json_spirit::mObject obj = top.get_obj();
    std::cout 
      << obj["text"     ].get_str()  << "\n"
      << obj["favorited"].get_bool() << "\n"
      << obj["source"   ].get_str()  << "\n"
      << obj["user"     ].get_obj()["name"].get_str();
}
Georg Fritzsche
A: 

you could try this http://qjson.sourceforge.net/

Olorin
A: 

Hi, did you get this to work? What parser did you end up using? I tried json_spirit -- it works fine on first few json strings but then goes belly up with following error: "value type is 6 not 2". Don't know what that means?

Azeem Michael
Hi dr.evil, I ended up using Jansson, which works perfectly and is quite up2date. You can find it here: http://www.digip.org/jansson/
pollux
got it to work. I just had to make sure value is not null: if(!obj["text"].is_null()) cout << obj["text"].get_str() << endl;thanks!
Azeem Michael
+1  A: 

I ended up using jansson which worked extremelly well!

pollux