gpt4 book ai didi

c - 使用 C 中的 libcURL 访问 google Places API 数据时打印出的网格化结果

转载 作者:行者123 更新时间:2023-11-30 17:55:20 25 4
gpt4 key购买 nike

在使用 C 语言的 libcURL 将 HTTP 数据从 google place API(文本搜索请求, https://developers.google.com/places/documentation/search )获取到 char* 时,我陷入了这个问题。

这是我的代码的一部分:

#include <stdio.h>
#include <curl/curl.h>

#define RESULT_SIZE 1024

char result[RESULT_SIZE];

static size_t write_data(char* buf, size_t size, size_t count, void *stream) {
int c;
for(c=0;c<size*count;c++)
result[c]=buf[c];
return size*count;
}

int main(void) {
int i;
char* query[300] = "";
CURL *curl;
CURLcode res;

//initializing..
for(i=0;i<RESULT_SIZE;i++)
result[i]=0;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();

// make query sentence
strcat(query,"https://maps.googleapis.com/maps/api/place/textsearc/json?query=40.690043,-74.045062&key=<myGoogleAPIkey>");

curl_easy_setopt(curl, CURLOPT_URL, query);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

res = curl_easy_perform(curl);

printf("%s\n", result");
}

我想要的结果是:

{
"html_attributions" : [],
"results" : [
{
"formatted_address" : "Statue of Liberty National Monument, Statue Of Liberty, 292 Madison Ave, New York, NY 10017, USA",
"geometry" : {
"location" : {
"lat" : 40.69004950,
"lng" : -74.04506750
},
"viewport" : {
"northeast" : {
"lat" : 40.69850950,
"lng" : -74.02906010
},
"southwest" : {
"lat" : 40.68158840,
"lng" : -74.06107489999999
}
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "c42384e74cfe05bd24d144b520b95cab496b9593",
"name" : "Statue of Liberty National Monument",
"reference" : "CpQBiwAAAJB0nFYotkps-eKyoe4WdSHfbKjGcQKSslzCtxpYrmZXS4-0jEXXz_iQV5ngCostzgcMQwrzLFFoJuY8OTlxltNs0GEVuslLNUjPSw4qMHKA-UIZ4_uPAbGONiLaYK5rW4UE--Ff0CTkaRTWsykQoEhJi8Vtekk1RgaexuJgjrsrWOf8HqCsw_mTXa24fqzYUhIQj-AwBAZy08dB0Y8jUZ0bERoUpKEkf-KXVCGrRGayir9y1KOnBpA",
"types" : [ "point_of_interest", "establishment" ]
}
],
"status" : "OK"
}

但是我的程序打印出:

PUIJhMF-RoUWJ53xFJbynmmPscOUglzfinXnoE",
"types" : [ "point_of_interest", "establishment" ]
}
],
"status" : "OK"
}
iberty, 292 Madison Ave, New York, NY 10017, USA",
"geometry" : {
"location" : {
"lat" : 40.69004950,
"lng" : -74.04506750
},
"viewport" : {
"northeast" : {
"lat" : 40.69850950,
"lng" : -74.02906010
},
"southwest" : {
"lat" : 40.68158840,
"lng" : -74.06107489999999
}
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "c42384e74cfe05bd24d144b520b95cab496b9593",
"name" : "Statue of Liberty National Monument",
"reference" : "CpQBigAAAIIwNFStfq0qjoQp-alxRtjFkaonf462dz_qHebtyT8zwc-WrtGdKmEWF-s05xhZasmbfG2wjqsWR_9UBvWfvo5hXTRTEhFCvD4AxdfJ5sWZIJ1_Rp2RpPdibI0aN30vkjXjATkyrv3W7T8pqZZnEU-zGvO_PhtZh05QJbp0Tw4sxKjgUBrfzw2sKCVXpUXQWBIQ_6rPxztrs4rSI

...像这样的混搭字符串。

我做错了什么吗?那么如何获取完整的JSON类型数据呢?或者有什么比用 C 编码更好的建议吗?(实际上,我已经厌倦了在 C..:() 中处理字符串格式

提前致谢。

最佳答案

首先尝试修复此错误:

printf("%s\n", result");

应该是

printf("%s\n", result);

PLUS... RESULT_SIZE 太小。增加它。

PLUS...每次获得数据 block 并调用写入函数时,结果数组将从第一个字节开始覆盖,而不是连接结果

关于c - 使用 C 中的 libcURL 访问 google Places API 数据时打印出的网格化结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14435274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com