json-c读写文件

代码只供参考。

  json_object *host_name = json_object_object_get(request, "hostName");
  json_object *blocked = json_object_object_get(request, "blocked");
  json_object *my_array = json_object_new_array();
  json_object *key_obj = json_object_new_object();
  json_object *new_device = json_object_new_object();  char *device_blocked = (char *)json_object_get_string(blocked);
  char *device_host_name = (char *)json_object_get_string(host_name);
  int i;
  if(access("/vendor/uf.conf",0) == 0)
  {
    my_array = json_object_from_file("/vendor/uf.conf");
    int exit=0;
    for(i=0; i < json_object_array_length(my_array); i++) {
      struct json_object *obj = json_object_array_get_idx(my_array, i);
      wlink_log_write(WLINK_LOG_DEBUG, "\t.......[%d]=%s.......\n", i, json_object_to_json_string(obj));
      key_obj = json_object_object_get(obj,"mac");
      if(!strcmp(json_object_to_json_string(mac_address),json_object_to_json_string(key_obj))){
        exit = 1;
  		json_object_object_add(obj,"name",json_object_new_string(device_host_name));
        json_object_object_add(obj,"block_all",json_object_new_string(device_blocked));
        break;
      }
    }
    if(exit == 0){  
      json_object_object_add(new_device,"name",json_object_new_string(device_host_name));
      json_object_object_add(new_device,"block_all",json_object_new_string(device_blocked));  
      json_object_array_add(my_array,new_device);
    }
  }
  else{
  	json_object_object_add(new_device,"name",json_object_new_string(device_host_name));
    json_object_object_add(new_device,"block_all",json_object_new_string(device_blocked));
    json_object_array_add(my_array,new_device);
  }
  wlink_log_write(WLINK_LOG_DEBUG, "\t......my_array=%s.....\n",json_object_to_json_string(my_array));
  json_object_to_file("/vendor/uf.conf", my_array);
  json_object_put(my_array);
  json_object_put(key_obj );
  json_object_put(new_device);

你可能感兴趣的:(web)