Tag: json

通过JSON检索数字并存储在变量中

好吧所以我对编程很新,只学了一点基本的C. https://api.vircurex.com/api/get_info_for_1_currency.json?base=DOGE&alt=BTC http://software.intel.com/en-us/html5/articles/how-to-access-JSON-data-in-HTML5-apps 基本上我想做的是从“ http://pastebin.com/4SPcTFbQ ” (链接1)中检索json并存储它在某种变量中返回的数字。 (有点像使用scanf()来检索数字并将其存储到变量中) 从我一直在研究它不能通过C完成,我相信必须通过JavaScript完成。 在这个网站上(参见pastebin Link 2),他们提供了这个例子, (参考pastebin Link 3)但是当我尝试将他们的示例json替换为vircurex时,它似乎不再起作用了。 任何帮助都将非常感谢! 这是一个例子: HTML Get JSON with padding Get JSON Get JSON with Access-Control-Allow-Origin header: * Get JSON Get JSON with NO Access-Control-Allow-Origin header Get JSON 使用Javascript function doJSON1() { $.getJSON(‘http://time.jsontest.com/?alloworigin=false&callback=?’, function (data) { alert(JSON.stringify(data)) }); } function doJSON2() { $.getJSON(‘http://time.jsontest.com/?alloworigin=true’, […]

json_decref()导致内存写入无效

当我调用json_decref()时遇到了一些问题 #include #include void main() { json_t *aa, *bb, *cc, *dd; aa = json_load_file (“/home/cuihaikuo/demo.json”, JSON_STRICT, NULL); bb = json_array_get (aa, 0); if (bb != NULL) json_decref (bb); if (aa != NULL) json_decref (aa); } 我使用vargrind运行程序,它说大小为8的无效写入 ==2641== Invalid write of size 8 ==2641== at 0x4E4201A: json_delete (in /usr/lib/x86_64-linux-gnu/libjansson.so.4.7.0) ==2641== by 0x4006C3: json_decref (in /home/cuihaikuo/chk/test/a) ==2641== […]

Lua嵌套从Lua到C的表

我很难将嵌套的Lua表转换为C json对象。 我正在使用LuaTableToJson(参见后面的代码),其中“index”是要检索的参数索引,PopOneArg例程根据其类型处理值。 当有一个嵌套表时,我想以递归方式调用LuaTableToJson,但是如果index = -1则不起作用。 问题:可以请某人点工作样本来检索从Lua到C的参数传递的嵌套表。或者有人可以在将嵌套表从Lua传递给C时解释堆栈结构。 谢谢 注意:对于C-> Lua,我有一个解决方案。 STATIC json_object *LuaTableToJson (lua_State* luaState, int index) { int idx; json_object *tableJ= json_object_new_object(); const char *key; char number[3]; lua_pushnil(luaState); // 1st key for (idx=1; lua_next(luaState, index) != 0; idx++) { // uses ‘key’ (at index -2) and ‘value’ (at index -1) if (lua_type(luaState,-2) == LUA_TSTRING) […]

使用YAJL在C中解析复杂的JSON子对象

我有YAJL解析我在包含的示例中给出的简单元素没有问题。 (字符串,整数,数组……) 示例代码可以在这里找到: http : //lloyd.github.io/yajl/yajl-2.0.1/example_2parse_config_8c-example.html 但现在我有这种类型的JSON对象: { “cmd”:2, “properties”: [ { “idx”:40, “val”:8813.602692 }, { “idx”:41, “val”:960 }, { “idx”:42, “val”:2 }, { “idx”:48, “val”:9 } ] } 我可以检索命令(参见链接示例中使用的变量的定义): const char * path[] = {“cmd”, (const char *) 0 }; yajl_val v = yajl_tree_get(ynode, path, yajl_t_number); if (v) *cmd = (commands)((int)YAJL_GET_INTEGER(v)); 我可以使用以下命令获取属性数组的引用: int ar_sz; […]

如何清理“json_object_new_string”创建的json对象?

我有以下代码,我想清理由json_object_new_string()创建的json对象。 #include #include int main() { /*Creating a json object*/ json_object * jobj = json_object_new_object(); /*Creating a json string*/ json_object *jstring = json_object_new_string(“Joys of Programming”); /*Form the json object*/ json_object_object_add(jobj,”Site Name”, jstring); /*Now printing the json object*/ printf (“The json object created: %sn”,json_object_to_json_string(jobj)); /* clean the json object */ json_object_put(jobj); } 行json_object_put(jobj); 清理jobj和jstring ? 或者我必须使用json_object_put(jstring);单独使用干净的jstring […]

使用curl和json-c从C调用json webservice

我需要调用一个接受json对象的restful webservice。 我能够找到像libcurl这样的库来从C应用程序调用web服务。 我还能够找到像json-c这样的库来在C中创建一个json对象。 libcurl接受一个字符串并将其发布到给定的webservice url。 无论如何,我可以发布使用json-c库创建的json对象,并使用curl库调用webservice。 如果有任何其他库允许我同时创建一个json对象并调用webservice或任何其他替代解决方案,请告诉我。 感谢您的帮助。

使用C的原始libcurl JSON PUT请求

我目前正在编写类似REST的客户端,只需要执行PUT请求。 问题: 运行程序并没有在URL的API上给我正确的结果,我不知道为什么。 使用curl_easy_perform(curl)不会在调用时抛出错误。 但是,不会在URL的API上生成预期结果。 使用curl_easy_send(curl,..,..,..)会抛出:不支持的协议错误 假设: 我假设我使用curl_easy_opts的顺序是个问题? 我甚至错过了几个关键线? 我一直在这里阅读其他人如何做PUT请求并一直在使用他们的方法。 计划摘要: 我的程序提示用户输入一些字符串/字符数据,从中我自己构造字符串,例如标题和有效负载。 标头和有效负载都是JSON格式,但有效负载只是一个字符串(在这种情况下,char * str =(char *)mallo …等)。 标题的构造方式如下所示。 我的标题正在使用 struct curl_slist *headers = NULL; headers = curl_slist_append(headers, “Accept: application/json”); //there is more content being appended to the header CURL函数调用: //init winsock stuff curl_global_init(CURL_GLOBAL_ALL); //get a curl handle curl = curl_easy_init(); if(curl){ //append the headers […]

内存泄漏使用JSON-C

我是JSON-C的新手,请查看我的示例代码,让我知道它会创建任何内存泄漏,如果是,那么如何释放JSON-C对象。 struct json_object *new_obj = NULL; new_obj = json_tokener_parse(strRawJSON); new_obj = json_object_object_get(new_obj, “FUU”); if(NULL == new_obj){ SYS_OUT(“\nFUU not found in JSON”); return NO; } new_obj = json_object_object_get(new_obj, “FOO”); // I m re-using new_obj, without free it? if(NULL == new_obj){ SYS_OUT(“\nFOO not found in JSON”); return NO; } // DO I need to clean new_obj, if […]

在C中的服务器和客户端应用程序中通过套接字发送和接收JSON

我想通过C语言编写的服务器 – 客户端应用程序中的套接字以JSON格式发送数据。 我使用json-c / libjson库来处理C应用程序中的JSON数据。 通过处理一些教程,我能够创建JSON对象并能够成功解析它。 现在我想使用JSON数据格式进行服务器客户端的通信。 这是我的服务器和客户端代码的一部分 server.c int main() { int listenfd = 0, connfd = 0; //related with the server struct sockaddr_in serv_addr; //json_object * jobj; uint8_t buf[158], i; memset(&buf, ‘0’, sizeof(buf)); listenfd = socket(AF_INET, SOCK_STREAM, 0); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(8888); bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); printf(“binding\n”); […]

使用cJSON读入JSON数组

我试图使用由Dave Gamble编写的cJSON库来读入以下JSON数组: “items”: [ { “name”: “command”, “index”: “X”, “optional”: “0” }, { “name”: “status”, “index”: “X”, “optional”: “0” } ] 通过阅读他的文档 ,我找到了阅读单个对象的方法,但没有关于数组的内容,我无法从给出的示例中推测如何做到这一点。 这是我正在尝试的: cJSON* request_json = NULL; cJSON* items = cJSON_CreateArray(); cJSON* name = NULL; cJSON* index = NULL; cJSON* optional = NULL; request_json = cJSON_Parse(request_body); items = cJSON_GetObjectItem(request_json, “items”); name = cJSON_GetObjectItem(items, “name”); […]