페이지 선택
Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Search in pages

MXOS JSON Parsing APIs

 

MXOS supports JSON format parsing for easy data transfer.

 

§ MXCHIP MXOS API Site 참조한다.

⊕ mxos-demos SDK를 참조한다.

 

 

 

1. JSON Parsing API  List

 

JSON Parsign API Descrtiption
json_object_get Incremental json_object reference count
json_object_put Decrement reference count of json_object
json_object_is_type Query if the json object is a given type
json_object_get_type Get the type of json object instance
json_object_to_json_string Convert a Json object instance from a type to a character
json_object_new_object Create a Json object
json_object_get_object Get a type hash table of a Json object
json_object_object_add Add a field name for a json object of the specified type
json_object_object_get Get a json object associated with a given field name
json_object_object_del Delete the given json object field
json_object_new_array Create a json object of type json_object_array
json_object_get_array Get a list of arrays of json_object_array type json objects
json_object_array_length Get the length of a json_object_array type json object
json_object_array_add Add an element to the end of a json_type_array type json object
json_object_array_put_idx Inserts or replaces an element of the specified index in the array, which is a json object of type json_type_array
json_object_array_get_idx Gets the element of the specified index in the array, which is a json object of type json_type_array
json_object_new_boolean Create a json object of type json_object_boolean
json_object_get_boolean Get the boolean value of a json object
json_object_new_int Create an empty json object of type json_object_int
json_object_new_int64 Create a json object of type json_object_int, 64 bits
json_object_get_int Get the integer value of a json object, type: json_object_int
json_object_get_int64 Get the integer value of a json object, type: json_object_int, 64 bits
json_object_new_double creates a json object of type json_object_double
json_object_get_double Get the value of a json object, type: json_object_double
json_object_new_string Create a json object of type json_object_string
json_object_new_string_len: creates a json object of the specified length, type: json_object_string
json_object_get_string Get the string value of the json object, object type: json_object_string
json_object_to_json_string_ex get the memory buffer information of the json object
json_object_get_string_len Get the string length of the json object, object type: json_object_string

 

 

 

 

2. Example

 

Example implementation: Output the parameters of the RGB light in a json format, parse the variables in the Json string, and print. code show as below:

 

 

json_op.c

 

#include "mxos.h"
#include "json_c/json.h"
#include "micokit_ext.h"
#define os_json_log(M, ...) custom_log("JSON", M, ##__VA_ARGS__)


void test_jsonc()  
{  
  /*control info*/
  bool rgb_sw = false;
  int  rgb_hue = 0;
  int  rgb_sat = 0;
  int  rgb_bri = 0;

  /*1:construct json object*/
  struct json_object *recv_json_object=NULL;
  recv_json_object=json_object_new_object();

  struct json_object *device_object=NULL;
  device_object=json_object_new_object();
  json_object_object_add(device_object, "Hardware", json_object_new_string("MiCOKit3288"));   
  json_object_object_add(device_object, "RGBSwitch", json_object_new_boolean(false)); 
  json_object_object_add(device_object, "RGBHues", json_object_new_int(0)); 
  json_object_object_add(device_object, "RGBSaturation", json_object_new_int(100));  
  json_object_object_add(device_object, "RGBBrightness", json_object_new_int(100)); 

  json_object_object_add(recv_json_object,"device_info",device_object);/*one pair K-V*/
  os_json_log("%s",json_object_to_json_string(recv_json_object));

  /*recv_json_object*/
  /*
  {"device_info": 
      {  "Hardware": "MiCOKit3288", 
         "RGBSwitch": false, 
         "RGBHues": 0, 
         "RGBSaturation": 100, 
         "RGBBrightness": 100 
      } 
  }
  */

  /*2:parse json object*/
  json_object* parse_json_object=json_object_object_get(recv_json_object,"device_info");
  /*get data one by one*/
  json_object_object_foreach(parse_json_object, key, val) {
          if(!strcmp(key, "RGBSwitch")){
            rgb_sw = json_object_get_boolean(val);
            os_json_log("rgb_sw=%d",rgb_sw);
          }
          else if(!strcmp(key, "RGBHues")){
            rgb_hue = json_object_get_int(val);
            os_json_log("rgb_hue=%d",rgb_hue);
          }
          else if(!strcmp(key, "RGBSaturation")){
            rgb_sat = json_object_get_int(val);
            os_json_log("rgb_sat=%d",rgb_sat);
          }
          else if(!strcmp(key, "RGBBrightness")){
            rgb_bri = json_object_get_int(val);
            os_json_log("rgb_bri=%d",rgb_bri);
          }
        }
    /*3:parse finished,free memory*/
    json_object_put(recv_json_object);/*free memory*/   
    recv_json_object=NULL;

    /*4:operate rgb*/
   os_json_log("control rgb led now");
   rgb_led_init();
   hsb2rgb_led_open(rgb_hue, rgb_sat, rgb_bri);/*turn red*/
}  


int application_start( void )
{  
    test_jsonc();  
    return 0;  
}  

 

 

 

 

이상 끝 —

Viewed Page List