json_object_get_double: Get double value of json object

Posted by Joys of Programming on June 1, 2010 in C/C++, JSON, Linux, json-c |

json_object_get_double() is used to get the double value of json object. The function takes a json_object as input and returns a double value.

#include <json/json.h>
#include <stdio.h>

void json_parse(json_object * jobj) {
  enum json_type type;
  json_object_object_foreach(jobj, key, val) {
    type = json_object_get_type(val);
    switch (type) {
      case json_type_double: printf("type: json_type_double, ");
                          printf("value: %lf\n", json_object_get_double(val));
                          break;
    }
  }
}
int main() {
  char * string = "{ \"PI\" : 3.14,\
                     \"random\" : 7e5,\
                     }";
  printf ("JSON string: %s\n", string);
  json_object * jobj = json_tokener_parse(string);
  json_parse(jobj);
}

Let’s compile the program. If you fail any compilation issues, refer the post.
On executing the program, we get the following output


$ ./a.out
JSON string: { "PI" : 3.14,                     "random" : 7e5,                     }
type: json_type_double, value: 3.140000
type: json_type_double, value: 700000.000000

The input to the program was


{
 "PI" : 3.14,
 "random" : 7e5
}

Related posts:

  1. json_object_new_double- Create a new double json object
  2. json_object_object_get: Get a json object
  3. json_object_get_array: Access an array JSON object
  4. json_object_get_int : Get integer value of a json object
  5. json_object_get_boolean : Get boolean value of a json object

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv Enabled

Copyright © 2009-2010 Joys of Programming All rights reserved.
Desk Mess Mirrored v1.7.2 theme from BuyNowShop.com.