如何拆分多行字符串并将它们存储到 C 中的 json 数组中
How to split multiple lines of string and store them into a json array in C
我有一个 bash 脚本,它以 Hostname IP MacAddr
格式输出字符串,并由我用 C 编写的脚本读取。我试图将这 3 个拆分成一个数组,并制作它以便我能够将它们存储到 Json-c 对象中以生成看起来像 {Clients: [{Hostname: Value, IP: Value, MacAddr: Value}]}
的东西。
目前我的程序能够逐行读取每个字符串并将其存储到一个数组中(该数组初始化错误只是为了测试目的,我将更改它):
int get_list_of_connected_clients(json_object *input, json_object *output) {
FILE *fp;
char path[1035];
int i = 0;
char a[2][100];
fp = popen("./Sample_Bash_Script_Test.sh", "r");
if (fp == NULL) {
printf("Failed To Run Script \n");
exit(1);
}
while (fgets(path, sizeof(path) - 1, fp) != NULL) {
stpcpy(a[i], path);
i++;
}
pclose(fp);
}
有谁能帮我解决这个问题并引导我朝着正确的方向前进吗? C 中的字符串操作对我来说相对较新,我仍在努力研究它!
编辑:
我的函数现在看起来像这样:
int get_list_of_connected_clients(json_object* input, json_object* output){
FILE *filepath;
char output_line[1035];
int index=0;
char arr_clients[30][100];
filepath = popen("./Sample_Bash_Script_Test.sh", "r");
if (filepath == NULL){
printf("Failed To Run Script \n");
exit(1);
}
while (fgets(output_line, sizeof(output_line)-1, filepath) != NULL){
stpcpy(arr_clients[index], output_line);
index++;
}
pclose(filepath);
/*Creating a json object*/
json_object * jobj = json_object_new_object();
/*Creating a json array*/
json_object *jarray = json_object_new_array();
json_object *jstring1[2][2];
for (int y=0; y < 2; y++) {
int x = 0;
char *p = strtok(arr_clients[y], " ");
char *array[2][3];
while (p != NULL) {
array[y][x++] = p;
p = strtok(NULL, " ");
}
for (x = 0; x < 3; ++x) {
jstring1[y][x] = json_object_new_string(array[y][x]);
/*Adding the above created json strings to the array*/
json_object_array_add(jarray,jstring1[y][x]);
}
}
/*Form the json object*/
json_object_object_add(jobj,"Clients", jarray);
/*Now printing the json object*/
printf ("%s",json_object_to_json_string(jobj));
return 0;
}
当我 运行 时,输出看起来像这样:{ "Clients": [ "Hostname", "192.168.1.18", "XX:XX:XX:XX", "Hostname", "192.168.1.13", "XX:XX:XX:XX" ] }
有人知道我做错了什么来阻止它在每个客户之后打破列表吗?即
{
"Clients" : [
{
"Hostname" : "example.com",
"IP" : "127.0.0.1",
"MacAddr" : "mactonight"
},
{
"Hostname" : "foo.biz",
"IP" : "0.0.0.0",
"MacAddr" : "12:34:56:78"
}
]
}
与其尝试将 JSON 构建为字符串,不如使用 json-glib to build the JSON for you. This is more flexible and will handle all sorts of edge cases. It provides JsonBuilder 等库来构建 JSON 结构。
我们从一个文件指针开始,其他东西应该打开文件。然后我们启动一个 JsonBuilder 并开始构建 JSON 结构,声明 { "Clients"
对象并启动数组。
JsonNode *bash_connected_clients_to_json(FILE *fp) {
JsonBuilder *builder = json_builder_new();
// { "Clients": [ ...
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "Clients");
json_builder_begin_array(builder);
现在我们读取每一行并将其和构建器发送到一个函数中以处理该行并将其添加到开放数组中。
char line[1024];
while (fgets(line, sizeof(line), fp) != NULL) {
bash_connected_clients_line_to_json(line, builder);
}
最后关闭数组和对象以及 return 我们刚刚构建的 JsonNode。
// ... ] }
json_builder_end_array(builder);
json_builder_end_object(builder);
return json_builder_get_root(builder);
}
然后就可以打印JsonNode了
int main() {
JsonNode *json = bash_connected_clients_to_json(stdin);
printf("%s", json_to_string(json, TRUE));
}
处理每一行都从解析它开始。这可以在各种被完成。 sscanf
工作正常。
void bash_connected_clients_line_to_json( const char *line, JsonBuilder *builder ) {
char hostname[1024], ip[1024], macaddr[1024];
if( sscanf(line, "%1023s %1023s %1023s", hostname, ip, macaddr) != 3 ) {
fprintf(stderr, "Could not parse line: '%s'\n", line);
return;
}
然后我们将一个 JSON 对象添加到我们已经打开的数组中,将我们的每个元素添加到对象中,然后关闭对象。
// { "Hostname": "foo", "IP", "bar", "MacAddr", "baz" }
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "Hostname");
json_builder_add_string_value(builder, hostname);
json_builder_set_member_name(builder, "IP");
json_builder_add_string_value(builder, ip);
json_builder_set_member_name(builder, "MacAddr");
json_builder_add_string_value(builder, macaddr);
json_builder_end_object(builder);
}
$ cat > test.txt
example.com 127.0.0.1 mactonight
foo.biz 0.0.0.0 12:34:56:78
$ ./test < test.txt
{
"Clients" : [
{
"Hostname" : "example.com",
"IP" : "127.0.0.1",
"MacAddr" : "mactonight"
},
{
"Hostname" : "foo.biz",
"IP" : "0.0.0.0",
"MacAddr" : "12:34:56:78"
}
]
}
或者几行Ruby.
require 'json'
clients = []
STDIN.each do |line|
fields = line.split(/\s+/)
clients << {
Hostname: fields[0],
IP: fields[1],
MacAddr: fields[2]
}
end
connections = {}
connections[:Clients] = clients
puts connections.to_json
对于json-c来说基本上是一样的。主要区别在于,它不是 bash_connected_clients_line_to_json
向构建器添加 JSON 对象,而是 return 一个 JSON 对象。
json_object* bash_connected_clients_line_to_json( const char *line ) {
char hostname[1024], ip[1024], macaddr[1024];
if( sscanf(line, "%1023s %1023s %1023s", hostname, ip, macaddr) != 3 ) {
fprintf(stderr, "Could not parse line: '%s'\n", line);
return NULL;
}
json_object *json = json_object_new_object();
json_object_object_add(json, "Hostname", json_object_new_string(hostname));
json_object_object_add(json, "IP", json_object_new_string(ip));
json_object_object_add(json, "MacAddr", json_object_new_string(macaddr));
return json;
}
然后将其添加到 JSON 客户端数组。
json_object *bash_connected_clients_to_json(FILE *fp) {
json_object *clients = json_object_new_array();
char line[1024];
while (fgets(line, sizeof(line), fp) != NULL) {
json_object_array_add(
clients,
bash_connected_clients_line_to_json(line)
);
}
json_object *json = json_object_new_object();
json_object_object_add(json, "Clients", clients);
return json;
}
我有一个 bash 脚本,它以 Hostname IP MacAddr
格式输出字符串,并由我用 C 编写的脚本读取。我试图将这 3 个拆分成一个数组,并制作它以便我能够将它们存储到 Json-c 对象中以生成看起来像 {Clients: [{Hostname: Value, IP: Value, MacAddr: Value}]}
的东西。
目前我的程序能够逐行读取每个字符串并将其存储到一个数组中(该数组初始化错误只是为了测试目的,我将更改它):
int get_list_of_connected_clients(json_object *input, json_object *output) {
FILE *fp;
char path[1035];
int i = 0;
char a[2][100];
fp = popen("./Sample_Bash_Script_Test.sh", "r");
if (fp == NULL) {
printf("Failed To Run Script \n");
exit(1);
}
while (fgets(path, sizeof(path) - 1, fp) != NULL) {
stpcpy(a[i], path);
i++;
}
pclose(fp);
}
有谁能帮我解决这个问题并引导我朝着正确的方向前进吗? C 中的字符串操作对我来说相对较新,我仍在努力研究它!
编辑:
我的函数现在看起来像这样:
int get_list_of_connected_clients(json_object* input, json_object* output){
FILE *filepath;
char output_line[1035];
int index=0;
char arr_clients[30][100];
filepath = popen("./Sample_Bash_Script_Test.sh", "r");
if (filepath == NULL){
printf("Failed To Run Script \n");
exit(1);
}
while (fgets(output_line, sizeof(output_line)-1, filepath) != NULL){
stpcpy(arr_clients[index], output_line);
index++;
}
pclose(filepath);
/*Creating a json object*/
json_object * jobj = json_object_new_object();
/*Creating a json array*/
json_object *jarray = json_object_new_array();
json_object *jstring1[2][2];
for (int y=0; y < 2; y++) {
int x = 0;
char *p = strtok(arr_clients[y], " ");
char *array[2][3];
while (p != NULL) {
array[y][x++] = p;
p = strtok(NULL, " ");
}
for (x = 0; x < 3; ++x) {
jstring1[y][x] = json_object_new_string(array[y][x]);
/*Adding the above created json strings to the array*/
json_object_array_add(jarray,jstring1[y][x]);
}
}
/*Form the json object*/
json_object_object_add(jobj,"Clients", jarray);
/*Now printing the json object*/
printf ("%s",json_object_to_json_string(jobj));
return 0;
}
当我 运行 时,输出看起来像这样:{ "Clients": [ "Hostname", "192.168.1.18", "XX:XX:XX:XX", "Hostname", "192.168.1.13", "XX:XX:XX:XX" ] }
有人知道我做错了什么来阻止它在每个客户之后打破列表吗?即
{
"Clients" : [
{
"Hostname" : "example.com",
"IP" : "127.0.0.1",
"MacAddr" : "mactonight"
},
{
"Hostname" : "foo.biz",
"IP" : "0.0.0.0",
"MacAddr" : "12:34:56:78"
}
]
}
与其尝试将 JSON 构建为字符串,不如使用 json-glib to build the JSON for you. This is more flexible and will handle all sorts of edge cases. It provides JsonBuilder 等库来构建 JSON 结构。
我们从一个文件指针开始,其他东西应该打开文件。然后我们启动一个 JsonBuilder 并开始构建 JSON 结构,声明 { "Clients"
对象并启动数组。
JsonNode *bash_connected_clients_to_json(FILE *fp) {
JsonBuilder *builder = json_builder_new();
// { "Clients": [ ...
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "Clients");
json_builder_begin_array(builder);
现在我们读取每一行并将其和构建器发送到一个函数中以处理该行并将其添加到开放数组中。
char line[1024];
while (fgets(line, sizeof(line), fp) != NULL) {
bash_connected_clients_line_to_json(line, builder);
}
最后关闭数组和对象以及 return 我们刚刚构建的 JsonNode。
// ... ] }
json_builder_end_array(builder);
json_builder_end_object(builder);
return json_builder_get_root(builder);
}
然后就可以打印JsonNode了
int main() {
JsonNode *json = bash_connected_clients_to_json(stdin);
printf("%s", json_to_string(json, TRUE));
}
处理每一行都从解析它开始。这可以在各种被完成。 sscanf
工作正常。
void bash_connected_clients_line_to_json( const char *line, JsonBuilder *builder ) {
char hostname[1024], ip[1024], macaddr[1024];
if( sscanf(line, "%1023s %1023s %1023s", hostname, ip, macaddr) != 3 ) {
fprintf(stderr, "Could not parse line: '%s'\n", line);
return;
}
然后我们将一个 JSON 对象添加到我们已经打开的数组中,将我们的每个元素添加到对象中,然后关闭对象。
// { "Hostname": "foo", "IP", "bar", "MacAddr", "baz" }
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "Hostname");
json_builder_add_string_value(builder, hostname);
json_builder_set_member_name(builder, "IP");
json_builder_add_string_value(builder, ip);
json_builder_set_member_name(builder, "MacAddr");
json_builder_add_string_value(builder, macaddr);
json_builder_end_object(builder);
}
$ cat > test.txt
example.com 127.0.0.1 mactonight
foo.biz 0.0.0.0 12:34:56:78
$ ./test < test.txt
{
"Clients" : [
{
"Hostname" : "example.com",
"IP" : "127.0.0.1",
"MacAddr" : "mactonight"
},
{
"Hostname" : "foo.biz",
"IP" : "0.0.0.0",
"MacAddr" : "12:34:56:78"
}
]
}
或者几行Ruby.
require 'json'
clients = []
STDIN.each do |line|
fields = line.split(/\s+/)
clients << {
Hostname: fields[0],
IP: fields[1],
MacAddr: fields[2]
}
end
connections = {}
connections[:Clients] = clients
puts connections.to_json
对于json-c来说基本上是一样的。主要区别在于,它不是 bash_connected_clients_line_to_json
向构建器添加 JSON 对象,而是 return 一个 JSON 对象。
json_object* bash_connected_clients_line_to_json( const char *line ) {
char hostname[1024], ip[1024], macaddr[1024];
if( sscanf(line, "%1023s %1023s %1023s", hostname, ip, macaddr) != 3 ) {
fprintf(stderr, "Could not parse line: '%s'\n", line);
return NULL;
}
json_object *json = json_object_new_object();
json_object_object_add(json, "Hostname", json_object_new_string(hostname));
json_object_object_add(json, "IP", json_object_new_string(ip));
json_object_object_add(json, "MacAddr", json_object_new_string(macaddr));
return json;
}
然后将其添加到 JSON 客户端数组。
json_object *bash_connected_clients_to_json(FILE *fp) {
json_object *clients = json_object_new_array();
char line[1024];
while (fgets(line, sizeof(line), fp) != NULL) {
json_object_array_add(
clients,
bash_connected_clients_line_to_json(line)
);
}
json_object *json = json_object_new_object();
json_object_object_add(json, "Clients", clients);
return json;
}