我在 C 中使用 libcURL将 HTTP 数据从 google 地方 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..:() 中使用字符串格式
提前致谢。