Ⅰ linux下怎麼使用jq工具把json解析成完整的欄位格式
1、通過JSON2.js中的JSON.parse()方法轉, 2、直接用javascript的eval()轉 3、自己寫邏輯解析對象字元串再封裝到一個對象裡面(這個自己寫很難寫得有通用性,建議採用方案一或者二)
Ⅱ 有哪些 C++ 的 JSON 庫比較好
我現在用的是cJSON這個JSON庫,在github上可以下載到,是開源的,Windows和Linux系統開發都可以用。而且是最輕量級的JSON庫,下載下來之後再解壓,只需要將裡面的cJSON.c和cJSON.h這兩個文件復制到你的項目(源碼)目錄就可以了(需要調用cJSON的那個代碼文件要加上一句#include "cJSON.h")。使用起來也超簡單,通過閱讀cJSON.h中的函數注釋和github上面的說明你就會知道怎麼用。
Ⅲ 如何在linux中使用命令行解析json文檔
開始 - 運行-CMD 在命令提示符下,輸入(引號裡面的內容) 「副教授的exe = exefile」。 >然後輸入: 「。assoc命令將DLL = dllfile」輸入 然後輸入: 「。assoc命令LNK = lnkfile」輸入 記事本
Ⅳ linux能夠識別的程序代碼是什麼
1、讀取文件全部內容
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
char ch;
FILE* fp;
unsigned long int count = 0;
char buf[1025] = {0};
// 這里要求我們在輸入兩個參數,第一個為 exe 路徑,第二個為 文件名
// 如 file_test.exe test.txt
if (argc != 2)
{
printf("Usage: %s filename\n", argv[0]);
exit(EXIT_FAILURE);
}
if ((fp = fopen(argv[1], "r")) == NULL)
{
// 如果文件打開失敗(通常是文件不存在),則結束程序
printf("Can't open %s\n", argv[1]);
exit(EXIT_FAILURE);
}
while ((ch = getc(fp)) != EOF && count < 1024)
{
// 顯示文本內容並計數
buf[count] = ch;
count++;
}
fclose(fp);
printf("%s\n", buf);
printf("File %s has %lu characters\n", argv[1], count);
return 0;
}
2、cJSON解析字元串
編譯選項要加 -lm
示例源串
{
"server": {
"nodes": [{
"ip": "10.76.76.190",
"port": 6379
}, {
"ip": "10.76.76.191",
"port": 6380
}, {
"ip": "10.76.76.192",
"port": 6381
}],
"password": "admin"
},
"isssl": true
}
登錄後復制
示例代碼(每一行記得判空,編譯時加-lm選項)
#include <stdio.h>
#include "cJSON.h"
int main(){
char c[] = "{\"server\":{\"nodes\":[{\"ip\":\"10.76.76.190\",\"port\":6379},{\"ip\":\"10.76.76.191\",\"port\":6380},{\"ip\":\"10.76.76.192\",\"port\":6381}],\"password\":\"admin\"},\"isssl\":true}";
cJSON* root = cJSON_Parse(c);
cJSON* json_server = cJSON_GetObjectItem(root, "server");
cJSON* json_isssl = cJSON_GetObjectItem(root, "isssl");
cJSON* json_password = cJSON_GetObjectItem(json_server, "password");
cJSON* json_nodes = cJSON_GetObjectItem(json_server, "nodes");
int i = 0;
for (; i != cJSON_GetArraySize(json_nodes); ++i) {
cJSON* each = cJSON_GetArrayItem(json_nodes, i);
cJSON* json_ip = cJSON_GetObjectItem(each, "ip");
cJSON* json_port = cJSON_GetObjectItem(each, "port");
printf("ip %s\n", json_ip->valuestring);
printf("port %d\n", json_port->valueint);
}
printf("password %s\n", json_password->valuestring);
printf("is ssl %s\n", json_isssl->valueint ? "true":"false");
cJSON_Delete(root);
}
3、curl拿到回傳的數據
編譯選項要加 -lcurl
頭部選項參考
https://curl.se/libcurl/c/curl_easy_setopt.html
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
struct string {
char* ptr;
size_t len;
};
void init_string(struct string* s) {
s->len = 0;
s->ptr = malloc(s->len + 1);
if (s->ptr == NULL) {
fprintf(stderr, "malloc() failed\n");
exit(EXIT_FAILURE);
}
s->ptr[0] = '\0';
}
size_t writefunc(void* ptr, size_t size, size_t nmemb, struct string* s) {
size_t new_len = s->len + size * nmemb;
s->ptr = realloc(s->ptr, new_len + 1);
if (s->ptr == NULL) {
fprintf(stderr, "realloc() failed\n");
exit(EXIT_FAILURE);
}
memcpy(s->ptr + s->len, ptr, size * nmemb);
s->ptr[new_len] = '\0';
s->len = new_len;
return size * nmemb;
}
int main(void) {
CURL* curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
struct string s;
init_string(&s);
curl_easy_setopt(curl, CURLOPT_URL, "https://www..com");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "test");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
printf("列印數據:\n%s", s.ptr);
free(s.ptr);
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
4、獲取隨機數
#include <stdio.h>
int main(int argc, char* argv[]){
srand((int)time(0));
//列印 32位 test 隨機數,並用16進製表示
int test2 = rand();
printf("%lx \n", test2);
// 列印 64位 test 隨機數,並用16進製表示, 016代表不足16位的部分,補零,llx代表列印64位的16進制數
unsigned long long test = ((unsigned long long)rand() << 32) + rand();
printf("%016llx \n", test);
return 0;
}
5、定義按位的結構體
總結,按照位定義結構體,一定要都按照位定義,不然會出現意想不到的錯誤,而且一定要定義無符號的
#include <stdio.h>
#pragma pack (1)
struct test1 {
unsigned char cloud_id:3;
unsigned char vendor_id:4;
unsigned short machine_id;
unsigned long long current_time:41;
};
struct test2 {
unsigned char cloud_id:3;
unsigned char vendor_id:4;
unsigned short machine_id:16;
unsigned long long current_time:41;
};
int main(int argc, char* argv[]){
printf("test1 大小 : %d \n", sizeof(struct test1));
printf("test2 大小 : %d \n", sizeof(struct test2));
return 0;
}
6、按位列印01位元組碼
#include <stdio.h>
// 修改type後面的類型,可以查看各種數的位元組碼
#define type unsigned int
void print_bin(type num)
{
int len = sizeof(type);
int n = 8;
int i, j, k;
unsigned char *p = (unsigned char*)&num + len -1;
for (i = 0; i < len; i++) //處理len個位元組
{
j = *(p - i); //取每個位元組的首地址
for ( k = 7; k >= 0; k--) //處理每個位元組的8個位
{
if (j & (1 << k))
printf("1");
else
printf("0");
}
printf(" ");
}
printf("\r\n");
}
int main(int argc, char* argv[]){
type a = 100;
print_bin(a);
return 0;
}
登錄後復制
7、列印16進制位元組碼
#include<stdio.h>
typedef unsigned char uint8;
#define HEXDUMP_LINE_LENGTH 16
void hex_mp(uint8* data, int length)
{
uint8* p = data;
int i, line, offset = 0;
while (offset < length)
{
printf("%04x ", offset);
line = length - offset;
if (line > HEXDUMP_LINE_LENGTH)
line = HEXDUMP_LINE_LENGTH;
for (i = 0; i < line; i++)
printf("%02x ", p[i]);
for (; i < HEXDUMP_LINE_LENGTH; i++)
printf(" ");
for (i = 0; i < line; i++)
printf("%c", (p[i] >= 0x20 && p[i] < 0x7F) ? p[i] : '.');
printf("\n");
offset += line;
p += line;
}
}
8、列印位元組碼簡易版
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main() {
unsigned char Buf[] = "123456";
unsigned int len = strlen(Buf);
char* Buf1 = (char*)malloc(len * 3 + 1);
int i = 0, k = 0;
for (; i < len * 3; i += 3, k++)
snprintf(&Buf1[i], 4, "%02x ", Buf[k]);
printf(Buf1);
free(Buf1);
}
9、逐級創建文件夾
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
int mkdir_recursively(char* dir) {
if (NULL == dir)
return 1;
size_t len = strlen(dir);
char* str = malloc(len + 1);
if (NULL == str)
return 1;
strcpy(str, dir);
int i = 0;
for (i = 0; i < len; i++) {
if (str[i] == '/') {
if (i == 0)
continue;
str[i] = '\0';
if (access(str, 0) != 0) {
if (mkdir(str, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
free(str);
return 1;
}
}
str[i] = '/';
}
}
if (len > 0 && access(str, 0) != 0) {
if (mkdir(str, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
free(str);
return 1;
}
}
free(str);
return 0;
}
int main() {
mkdir_recursively("/home/test/abc/edf")
Ⅳ PHP curl獲取的遠程數據,json_decode後返回空
嘗試一下把輸出文件定義為 json 文件。
header("Cache-Control: private");
header("Content-type: application/json; charset=utf-8");