python,import json,,json_data = '{"name": "John", "age": 30}',data = json.loads(json_data),name = data["name"],print(name) # 输出: John,
``c,#include,#include,#include "cJSON.h",,int main() {, const char *json_string = "[{\"name\": \"John\", \"age\": 30}, {\"name\": \"Jane\", \"age\": 25}]";, cJSON *json_array = cJSON_Parse(json_string);, if (json_array == NULL) {, printf("Error parsing JSON\n");, return 1;, },, int array_size = cJSON_GetArraySize(json_array);, for (int i = 0; i< array_size; i++) {, cJSON *item = cJSON_GetArrayItem(json_array, i);, if (item == NULL) continue;,, cJSON *name = cJSON_GetObjectItem(item, "name");, cJSON *age = cJSON_GetObjectItem(item, "age");,, if (cJSON_IsString(name) && (name->valuestring != NULL)) {, printf("Name: %s\n", name->valuestring);, }, if (cJSON_IsNumber(age)) {, printf("Age: %d\n", age->valueint);, }, },, cJSON_Delete(json_array);, return 0;,},
``,,这个代码片段展示了如何使用cJSON库解析一个包含多个对象的JSON数组,并打印每个对象中的“name”和“age”字段。Powered By Z-BlogPHP 1.7.3
Copyright Your WebSite.Some Rights Reserved.