|
6 | 6 |
|
7 | 7 | #include "json.h"
|
8 | 8 |
|
| 9 | +static int sort_fn (const void *j1, const void *j2) |
| 10 | +{ |
| 11 | + json_object **jso1, **jso2; |
| 12 | + int i1, i2; |
| 13 | + |
| 14 | + jso1 = j1; |
| 15 | + jso2 = j2; |
| 16 | + if (!*jso1 && !*jso2) { |
| 17 | + return 0; |
| 18 | + } |
| 19 | + if (!*jso1) { |
| 20 | + return -1; |
| 21 | + } |
| 22 | + if (!*jso2) { |
| 23 | + return 1; |
| 24 | + } |
| 25 | + |
| 26 | + i1 = json_object_get_int(*jso1); |
| 27 | + i2 = json_object_get_int(*jso2); |
| 28 | + |
| 29 | + return i1 - i2; |
| 30 | +} |
| 31 | + |
9 | 32 | int main(int argc, char **argv)
|
10 | 33 | {
|
11 | 34 | json_tokener *tok;
|
@@ -45,6 +68,27 @@ int main(int argc, char **argv)
|
45 | 68 | }
|
46 | 69 | printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
|
47 | 70 |
|
| 71 | + json_object_put(my_array); |
| 72 | + |
| 73 | + my_array = json_object_new_array(); |
| 74 | + json_object_array_add(my_array, json_object_new_int(3)); |
| 75 | + json_object_array_add(my_array, json_object_new_int(1)); |
| 76 | + json_object_array_add(my_array, json_object_new_int(2)); |
| 77 | + json_object_array_put_idx(my_array, 4, json_object_new_int(0)); |
| 78 | + printf("my_array=\n"); |
| 79 | + for(i=0; i < json_object_array_length(my_array); i++) { |
| 80 | + json_object *obj = json_object_array_get_idx(my_array, i); |
| 81 | + printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); |
| 82 | + } |
| 83 | + printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); |
| 84 | + json_object_array_sort(my_array, sort_fn); |
| 85 | + printf("my_array=\n"); |
| 86 | + for(i=0; i < json_object_array_length(my_array); i++) { |
| 87 | + json_object *obj = json_object_array_get_idx(my_array, i); |
| 88 | + printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); |
| 89 | + } |
| 90 | + printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); |
| 91 | + |
48 | 92 | my_object = json_object_new_object();
|
49 | 93 | json_object_object_add(my_object, "abc", json_object_new_int(12));
|
50 | 94 | json_object_object_add(my_object, "foo", json_object_new_string("bar"));
|
|
0 commit comments