static json_t *OutputStats2Json(json_t *js, const char *key) { void *iter; const char *dot = index(key, '.'); if (dot == NULL) return NULL; size_t predot_len = (dot - key) + 1; char s[predot_len]; strlcpy(s, key, predot_len); iter = json_object_iter_at(js, s); const char *s2 = index(dot+1, '.'); json_t *value = json_object_iter_value(iter); if (value == NULL) { value = json_object(); json_object_set_new(js, s, value); } if (s2 != NULL) { return OutputStats2Json(value, &key[dot-key+1]); } return value; }
//native Handle:json_object_iter_at(Handle:hObj, const String:key[]); static cell_t Native_json_object_iter_at(IPluginContext *pContext, const cell_t *params) { HandleError err; HandleSecurity sec; sec.pOwner = NULL; sec.pIdentity = myself->GetIdentity(); // Param 1 json_t *object; Handle_t hndlObject = static_cast<Handle_t>(params[1]); if ((err=g_pHandleSys->ReadHandle(hndlObject, htJanssonObject, &sec, (void **)&object)) != HandleError_None) { return pContext->ThrowNativeError("Invalid <Object> handle %x (error %d)", hndlObject, err); } // Param 2 char *key; pContext->LocalToString(params[2], &key); void *iter = json_object_iter_at(object, key); Handle_t hndlResult = g_pHandleSys->CreateHandle(htJanssonIterator, iter, pContext->GetIdentity(), myself->GetIdentity(), NULL); if(hndlResult == BAD_HANDLE) { pContext->ThrowNativeError("Could not create handle for JSON Iterator."); return BAD_HANDLE; } return hndlResult; }
/* 得到策略信息 */ struct policymsg get_policy_json (char *jsonfile) { struct policymsg policyinfo; int i, size; void *iter; json_t *object; json_t *iter_values; json_error_t error; object = json_object (); /* 读取策略 */ object = json_load_file (jsonfile, 0, &error); policyinfo.size = json_object_size (object); #if 0 //size = json_object_size (object); //printf("size=%d\n", size); /* 取出object中的值 */ //struct policy iter_get_value(json_t *object) char *result; result = json_dumps (object, JSON_PRESERVE_ORDER); printf ("result=%s\n", result); /* 判断读取的jansson类型 */ printf ("判断是什么类型\n"); my_json_type (object); printf ("result_size = %d\n", strlen (result)); #endif //printf("得到策略值\n"); iter = json_object_iter (object); i = 0; while (1) { strcpy (policyinfo.keyword[i], json_object_iter_key (iter)); iter_values = json_object_iter_value (iter); strcpy (policyinfo.keycount[i], json_string_value (iter_values)); //printf("values[%d]=%s\n", i,json_string_value(iter_values[i])); if ((iter = json_object_iter_next (object, iter)) == NULL) { //printf("iterate end\n"); break; } i++; } #if 0 iter = json_object_iter_at (object, "b"); if (iter) { iter_keys[i] = json_object_iter_key (iter); iter_values[i] = json_object_iter_value (iter); printf ("values[%d]=%s\n", i, json_string_value (iter_values[i])); } #endif json_decref (object); return policyinfo; }
const char *JSON::getObjectNextKey(const char *key) { if (!_json) { return ""; } void *iter = json_object_iter_next(_json, json_object_iter_at(_json, key)); if (!iter) { return ""; } return json_object_iter_key(iter); }
static void test_iterators() { int i; json_t *object, *foo, *bar, *baz; const char *iter_keys[3]; int have_key[3] = { 0, 0, 0 }; json_t *iter_values[3]; void *iter; if(json_object_iter(NULL)) fail("able to iterate over NULL"); if(json_object_iter_next(NULL, NULL)) fail("able to increment an iterator on a NULL object"); object = json_object(); foo = json_string("foo"); bar = json_string("bar"); baz = json_string("baz"); if(!object || !foo || !bar || !bar) fail("unable to create values"); if(json_object_iter_next(object, NULL)) fail("able to increment a NULL iterator"); if(json_object_set(object, "a", foo) || json_object_set(object, "b", bar) || json_object_set(object, "c", baz)) fail("unable to populate object"); iter = json_object_iter(object); if(!iter) fail("unable to get iterator"); iter_keys[0] = json_object_iter_key(iter); iter_values[0] = json_object_iter_value(iter); iter = json_object_iter_next(object, iter); if(!iter) fail("unable to increment iterator"); iter_keys[1] = json_object_iter_key(iter); iter_values[1] = json_object_iter_value(iter); printf("values=%s\n", json_string_value(iter_values[1])); iter = json_object_iter_next(object, iter); if(!iter) fail("unable to increment iterator"); iter_keys[2] = json_object_iter_key(iter); iter_values[2] = json_object_iter_value(iter); if(json_object_iter_next(object, iter) != NULL) fail("able to iterate over the end"); /* Check that keys have correct values */ for (i = 0; i < 3; i++) { if (strcmp(iter_keys[i], "a") == 0) { if (iter_values[i] != foo) fail("wrong value for iter key a"); else have_key[0] = 1; } else if (strcmp(iter_keys[i], "b") == 0) { if (iter_values[i] != bar) fail("wrong value for iter key b"); else have_key[1] = 1; } else if (strcmp(iter_keys[i], "c") == 0) { if (iter_values[i] != baz) fail("wrong value for iter key c"); else have_key[2] = 1; } } /* Check that we got all keys */ for(i = 0; i < 3; i++) { if(!have_key[i]) fail("a key wasn't iterated over"); } if(json_object_iter_at(object, "foo")) fail("json_object_iter_at() succeeds for non-existent key"); iter = json_object_iter_at(object, "b"); if(!iter) fail("json_object_iter_at() fails for an existing key"); if(strcmp(json_object_iter_key(iter), "b")) fail("iterating failed: wrong key"); if(json_object_iter_value(iter) != bar) fail("iterating failed: wrong value"); if(json_object_iter_set(object, iter, baz)) fail("unable to set value at iterator"); if(strcmp(json_object_iter_key(iter), "b")) fail("json_object_iter_key() fails after json_object_iter_set()"); if(json_object_iter_value(iter) != baz) fail("json_object_iter_value() fails after json_object_iter_set()"); if(json_object_get(object, "b") != baz) fail("json_object_get() fails after json_object_iter_set()"); json_decref(object); json_decref(foo); json_decref(bar); json_decref(baz); }
static void test_iterators() { json_t *object, *foo, *bar, *baz; void *iter; if(json_object_iter(NULL)) fail("able to iterate over NULL"); if(json_object_iter_next(NULL, NULL)) fail("able to increment an iterator on a NULL object"); object = json_object(); foo = json_string("foo"); bar = json_string("bar"); baz = json_string("baz"); if(!object || !foo || !bar || !bar) fail("unable to create values"); if(json_object_iter_next(object, NULL)) fail("able to increment a NULL iterator"); if(json_object_set(object, "a", foo) || json_object_set(object, "b", bar) || json_object_set(object, "c", baz)) fail("unable to populate object"); iter = json_object_iter(object); if(!iter) fail("unable to get iterator"); if(strcmp(json_object_iter_key(iter), "a")) fail("iterating failed: wrong key"); if(json_object_iter_value(iter) != foo) fail("iterating failed: wrong value"); iter = json_object_iter_next(object, iter); if(!iter) fail("unable to increment iterator"); if(strcmp(json_object_iter_key(iter), "b")) fail("iterating failed: wrong key"); if(json_object_iter_value(iter) != bar) fail("iterating failed: wrong value"); iter = json_object_iter_next(object, iter); if(!iter) fail("unable to increment iterator"); if(strcmp(json_object_iter_key(iter), "c")) fail("iterating failed: wrong key"); if(json_object_iter_value(iter) != baz) fail("iterating failed: wrong value"); if(json_object_iter_next(object, iter) != NULL) fail("able to iterate over the end"); if(json_object_iter_at(object, "foo")) fail("json_object_iter_at() succeeds for non-existent key"); iter = json_object_iter_at(object, "b"); if(!iter) fail("json_object_iter_at() fails for an existing key"); if(strcmp(json_object_iter_key(iter), "b")) fail("iterating failed: wrong key"); if(json_object_iter_value(iter) != bar) fail("iterating failed: wrong value"); iter = json_object_iter_next(object, iter); if(!iter) fail("unable to increment iterator"); if(strcmp(json_object_iter_key(iter), "c")) fail("iterating failed: wrong key"); if(json_object_iter_value(iter) != baz) fail("iterating failed: wrong value"); if(json_object_iter_set(object, iter, bar)) fail("unable to set value at iterator"); if(strcmp(json_object_iter_key(iter), "c")) fail("json_object_iter_key() fails after json_object_iter_set()"); if(json_object_iter_value(iter) != bar) fail("json_object_iter_value() fails after json_object_iter_set()"); if(json_object_get(object, "c") != bar) fail("json_object_get() fails after json_object_iter_set()"); json_decref(object); json_decref(foo); json_decref(bar); json_decref(baz); }
struct ast_json_iter *ast_json_object_iter_at(struct ast_json *object, const char *key) { return json_object_iter_at((json_t *)object, key); }
/* 获取每个元素的值, 返回元素的结构体 */ struct policymsg iter_get_value(json_t *object) { struct policymsg p; int i; json_t *iter_values[3]; const char *iter_keys[3]; void *iter; printf("判断是不是object类型\n"); my_json_type(object); //printf("........\n"); #if 0 if(json_object_iter(NULL)) { printf("iterate NULL\n"); } if(json_object_iter_next(NULL, NULL)) { printf("iterator a NULL object\n"); } #endif iter = json_object_iter(object); i = 0; while(1) { iter_keys[i] = json_object_iter_key(iter); iter_values[i] = json_object_iter_value(iter); //printf("values[%d]=%s\n", i,json_string_value(iter_values[i])); if((iter = json_object_iter_next(object, iter)) == NULL) { //printf("iterate end\n"); break; } i++; } i=4; iter = json_object_iter_at(object, "b"); if(iter) { iter_keys[i] = json_object_iter_key(iter); iter_values[i] = json_object_iter_value(iter); printf("values[%d]=%s\n", i,json_string_value(iter_values[i])); //printf("json_object_iter_at() printfs for an existing key\n"); } #if 0 if(json_object_iter_set(object, iter, baz)) printf("unable to set value at iterator"); if(strcmp(json_object_iter_key(iter), "b")) printf("json_object_iter_key() printfs after json_object_iter_set()"); if(json_object_iter_value(iter) != baz) printf("json_object_iter_value() printfs after json_object_iter_set()"); if(json_object_get(object, "b") != baz) printf("json_object_get() printfs after json_object_iter_set()"); #endif json_decref(object); return p; }
void *la_codec_object_iter_at(la_codec_value_t *object, const char *key) { return json_object_iter_at((json_t *) object, key); }
int arduinoEvent(char *buf, config_t *cfg, struct mosquitto *mosq) { if(strlen(buf)<2) return ERR_NO_JSON; json_error_t error; json_t *root = json_loads(buf, 0, &error); if(root == NULL) { DBG("<%s>\n", buf); ERR("error: on line %d: %s\n", error.line, error.text); return ERR_NO_JSON; } void *id = json_object_iter_at(root, "id"); if(id == NULL) { json_decref(root); return ERR_NO_ID; } json_t *json_device = json_object_iter_value(id); const char *device = json_string_value(json_device); char topic[255]; snprintf(topic, 255, "%s/raw", device); mosquitto_publish(mosq, NULL, topic, strlen(buf), strtok(buf,"\n"), 0, false); void *code = json_object_iter_at(root, "code"); if(code == NULL) { json_decref(root); return ERR_NO_ID; } json_t *json_code = json_object_iter_value(code); if(json_integer_value(json_code) == 200) { time_t now; now = time(NULL); snprintf(topic, 255, "%s/timestamp", device); char payload[20]; strftime(payload, 20, "%Y-%m-%d %H:%M:%S", localtime(&now)); mosquitto_publish(mosq, NULL, topic, strlen(payload), payload, 0, true); }; const char *key; json_t *value; void *iter = json_object_iter(root); while(iter) { key = json_object_iter_key(iter); value = json_object_iter_value(iter); /* use key and value ... */ char payload[2048]; snprintf(topic, 255, "%s/%s", device, key); if(json_is_integer(value)) { snprintf(payload, 2048, "%lld", json_integer_value(value)); } else if(json_is_real(value)) { snprintf(payload, 2048, "%f", json_real_value(value)); } else if(json_is_boolean(value)) { if(json_is_true(value)) snprintf(payload, 2048, "true"); else snprintf(payload, 2048, "false"); } else { snprintf(payload, 2048, "%s", json_string_value(value)); } mosquitto_publish(mosq, NULL, topic, strlen(payload), payload, 0, true); // DBG("%s -> %s\n", topic, payload); iter = json_object_iter_next(root, iter); } json_decref(root); return 0; }