Skip to content

Instantly share code, notes, and snippets.

@mkamilov
Created July 31, 2017 08:14
Show Gist options
  • Save mkamilov/e250602ba276f872354cb7acc4724fb2 to your computer and use it in GitHub Desktop.
Save mkamilov/e250602ba276f872354cb7acc4724fb2 to your computer and use it in GitHub Desktop.
json-glib with jsonobject. Can convert nested json as well
cmake_minimum_required (VERSION 2.4)
project (json-glib-demo)
find_package(PkgConfig REQUIRED)
pkg_search_module(JSONGLIB REQUIRED json-glib-1.0)
add_executable (json-glib-demo jsontest.cpp)
include_directories(${JSONGLIB_INCLUDE_DIRS})
target_link_libraries(json-glib-demo ${JSONGLIB_LIBRARIES})
#include <stdio.h>
#include <string>
#include <glib-object.h>
#include <json-glib/json-glib.h>
int main()
{
std::string data = "{\"response\": {\"reason\": \"get line info success.\",\"result\": \"true\"}}";
JsonParser *parser = json_parser_new ();
GError* err = NULL;
if (!json_parser_load_from_data (parser, data.c_str(), -1, &err)) {
printf("error in parsing json data %s", err->message);
g_error_free (err);
g_object_unref (parser);
return -1;
}
JsonObject *object = json_node_get_object(json_parser_get_root(parser));
JsonObject* response = json_object_get_object_member(object, "response");
GList *mylist = json_object_get_members(response);
while (mylist != NULL) {
printf("list= %s\n", (const char*)mylist->data);
//JsonObject *each_object = json_object_get_object_member(response, (const char*)mylist->data);
printf("reason = %s\n", json_object_get_string_member(response, (const char*)mylist->data));
mylist = mylist->next;
}
g_object_unref (parser);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment