我有一个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 等库为您构建 JSON。这更灵活,可以处理各种边缘情况。它提供了 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);
}
最后关闭数组和对象,并返回我们刚刚构建的 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 对象添加到构建器,而是返回一个 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;
}