我有一个JSON字符串,我想将其转换为整数数组。我有一个JSON解析器,但我无法理解如何实现我的结果。我的代码是:
#include <json/json.h>
#include <stdio.h>
/*printing the value corresponding to boolean, double, integer and strings*/
void print_json_value(json_object *jobj){
enum json_type type;
printf("type: ",type);
type = json_object_get_type(jobj); /*Getting the type of the json object*/
switch (type) {
case json_type_boolean: printf("json_type_boolean\n");
printf("value: %s\n", json_object_get_boolean(jobj)? "true": "false");
break;
case json_type_double: printf("json_type_double\n");
printf(" value: %lf\n", json_object_get_double(jobj));
break;
case json_type_int: printf("json_type_int\n");
printf(" value: %d\n", json_object_get_int(jobj));
break;
case json_type_string: printf("json_type_string\n");
printf(" value: %s\n", json_object_get_string(jobj));
break;
}
}
void json_parse_array( json_object *jobj, char *key) {
void json_parse(json_object * jobj); /*Forward Declaration*/
enum json_type type;
json_object *jarray = jobj; /*Simply get the array*/
if(key) {
jarray = json_object_object_get(jobj, key); /*Getting the array if it is a key value pair*/
}
int arraylen = json_object_array_length(jarray); /*Getting the length of the array*/
printf("Array Length: %d\n",arraylen);
int i;
json_object * jvalue;
for (i=0; i< arraylen; i++){
jvalue = json_object_array_get_idx(jarray, i); /*Getting the array element at position i*/
type = json_object_get_type(jvalue);
if (type == json_type_array) {
json_parse_array(jvalue, NULL);
}
else if (type != json_type_object) {
printf("value[%d]: ",i);
print_json_value(jvalue);
}
else {
json_parse(jvalue);
}
}
}
/*Parsing the json object*/
void json_parse(json_object * jobj) {
enum json_type type;
json_object_object_foreach(jobj, key, val) { /*Passing through every array element*/
printf("type: ",type);
type = json_object_get_type(val);
switch (type) {
case json_type_boolean:
case json_type_double:
case json_type_int:
case json_type_string: print_json_value(val);
break;
case json_type_object: printf("json_type_object\n");
jobj = json_object_object_get(jobj, key);
json_parse(jobj);
break;
case json_type_array: printf("type: json_type_array, ");
json_parse_array(jobj, key);
break;
}
}
}
int main() {
char * string = "{\"2\":[1,38,12,21,20,18,14,12],\"1\":[2,3]}";
printf("JSON string: %s\n", string);
json_object * jobj = json_tokener_parse(string);
json_parse(jobj);
}
上面的代码我发现它在线,但是这里作者将字符串解析为json obeject * jarray,但是我想要它是一个整数数组而不是json对象。任何帮助,将不胜感激。提前谢谢。
答案 0 :(得分:0)
在您的示例中,"test"
中有以下行:
"scripts": [
"node_modules/aframe/dist/aframe-v1.0.0.min.js"
],
在此处调用所需的函数,该函数以print_json_value()
的形式返回值,即printf(" value: %d\n", json_object_get_int(jobj));
。
设计您自己的函数来解析数组,并使用上述函数将值存储在数组中。