我收到了来自YouTube JSONC api的字符串,但持续时间是一个完整的数字,即2321而不是23:21或2而不是0:02.我该如何解决这个问题?
编辑:
int duration = [videos valueForKey:@"duration"];
int minutes = duration / 60;
int seconds = duration % 60;
NSString *time = [NSString stringWithFormat:@"%d:%02d", minutes, seconds];
Run Code Online (Sandbox Code Playgroud) 我是C语言的新手.我需要附加两个json_object用json-c库创建的数组.这是我的代码:
struct json_object *obj1,*array1,*array2,*array3;
obj1 = json_object_new_object();
array1 = json_object_new_array();
json_object_array_add(array1, json_object_new_int(1));
json_object_array_add(array1, json_object_new_int(2));
json_object_array_add(array1, json_object_new_int(3));
json_object_object_add(obj1, "array1", array1);
array2 = json_object_new_array();
json_object_array_add(array2, json_object_new_int(4));
json_object_array_add(array2, json_object_new_int(5));
json_object_array_add(array2, json_object_new_int(6));
json_object_object_add(obj1, "array2", array2);
json_object_object_add(obj1, "array3", array1+array2);
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
错误:无效操作数到二进制+(有'struct json_object*'和'struct json_object*')json_object_object_add(obj1,"array3",array1 + array2);
这不可能吗?我需要像这样的输出:
{
"array1:[1,2,3],
"array2":[4,5,6],
"array3":[1,2,3,4,5,6]
}
Run Code Online (Sandbox Code Playgroud)
或者是否可以将正常的C整数数组添加到Json,就像这样
int32_t smp[100]={0};
smp[0] = 1;
smp[1] = 2;
smp[2] = 3;
smp[3] = 4;
smp[4] = 5;
smp[5] = 6;
json_object_object_add(obj1, "array3", smp);
Run Code Online (Sandbox Code Playgroud) 我是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 yes then how ??
Run Code Online (Sandbox Code Playgroud)
我是否需要清理new_obj,如果是,那么如何.有人可以帮助理解如何进行内存管理JSON-C.
提前致谢
我正在尝试编译json-c-0.9测试二进制文件,同时静态链接到libjson.a,我已经构建并坐在/path/to/json-c-0.9/lib:
$ gcc -g -v -Wall -std=gnu99 -static -L/path/to/json-c-0.9/lib -ljson test1.c -o test1
Run Code Online (Sandbox Code Playgroud)
我收到了很多形式的错误:
/path/to/json-c-0.9/test1.c:17: undefined reference to `json_object_new_string'
/path/to/json-c-0.9/test1.c:18: undefined reference to `json_object_get_string'
/path/to/json-c-0.9/test1.c:19: undefined reference to `json_object_to_json_string'
/path/to/json-c-0.9/test1.c:20: undefined reference to `json_object_put'
/path/to/json-c-0.9/test1.c:22: undefined reference to `json_object_new_string'
etc.
Run Code Online (Sandbox Code Playgroud)
在尝试编译测试二进制文件时我缺少什么?谢谢你的建议.
我正在使用 json-c 库将 json-object 发送到客户端。我注意到没有本机函数来释放 json_object_to_json_string 分配的内存。该库会自动释放它吗?或者我必须“free(str)”以避免内存泄漏?我试图阅读它的源代码,但它让我失去知觉......所以有人知道这一点吗?
我有以下代码,我想清理由创建的json对象json_object_new_string().
#include <json/json.h>
#include <stdio.h>
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);
}
Run Code Online (Sandbox Code Playgroud)
是否行json_object_put(jobj);清洗都jobj和jstring?
或者我必须jstring单独干净json_object_put(jstring);?
编辑
问题2
如果jstring以这种方式创建一个函数会有什么行为?
#include <json/json.h>
#include <stdio.h>
static void my_json_add_obj(json_object *jobj, …Run Code Online (Sandbox Code Playgroud) 当我的程序像这样在 json 中保存一些东西时:
json_object_object_add(jObj_my, "cats/dogs", json_object_new_double(cats/dogs));
Run Code Online (Sandbox Code Playgroud)
.json 文件中的结果是:
"cats\/dogs" : some_double_number
Run Code Online (Sandbox Code Playgroud)
我怎样才能避免它打印"\/"而不是打印"/"?
下面的代码编译并打印响应。
我的问题是:作为代表对象的字符串的响应,如何将“res”转换为字符串或直接转换为 JSON 对象?
#include <stdio.h>
#include <curl/curl.h>
#include <json-c/json.h>
int main(int argc, char **argv) {
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/system/genpass");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "length: 20");
headers = curl_slist_append(headers, "numbers: true");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
res = curl_easy_perform(curl);
printf("%u",res);
}
curl_easy_cleanup(curl);
}
Run Code Online (Sandbox Code Playgroud) 在 Macbook 上,我正在用 json-c( https://github.com/json-c/json-c )做一些事情
gcc a.c
a.c:1:10: fatal error: 'json.h' file not found
#include "json.h"
^
1 error generated.`>
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,
它打印出错误,但我json.h在包含文件中有文件。
> cd /usr/local/include/json-c/
> ls
arraylist.h json_config.h json_tokener.h
bits.h json_inttypes.h json_util.h
debug.h json_object.h linkhash.h
json.h json_object_iterator.h printbuf.h
json_c_version.h json_object_private.h random_seed.h
Run Code Online (Sandbox Code Playgroud)
/usr/include/json-c/json.h 肯定存在
我在谷歌搜索了很多。我想以编程方式检查下面的字符串是否是 C 中的有效 JSON。我该怎么做?(我目前正在使用json-c图书馆。)
char * string = "{
number1 : 100,
number2 : 10,
files : [ c , c++, java,PHP,java,PHP ],
random: [123567876523333,908,988]
}";
Run Code Online (Sandbox Code Playgroud)
该库没有检查字符串是否为有效 JSON 的功能。
json-c ×10
c ×8
json ×5
arrays ×1
curl ×1
escaping ×1
gcc ×1
header-files ×1
ios ×1
jsonobject ×1
memory-leaks ×1
parsing ×1
time ×1
youtube ×1