动态获取省份的位置的

http://pv.sohu.com/cityjson?ie=utf-8

#include <stdio.h>
#include <string.h>
#include "curl/curl.h"
#include "json.h"

size_t writefuc( char *ptr, size_t size, size_t nmemb, char *stream)
{
	if( !ptr || !stream )
		return -1;
	
	unsigned long sizes = size * nmemb;
	memcpy(stream, ptr, sizes);
	return sizes;
}



int main()
{
	CURL *curl = NULL;
	char buffer[256] = "";
	char procvince[64] = "";
	char *pstr = NULL;
	json_object *jsonstr = NULL;
	json_object *item = NULL;
	CURLcode res;
	curl = curl_easy_init();
	if(curl)
	{
		curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5);
		curl_easy_setopt(curl, CURLOPT_URL, "http://pv.sohu.com/cityjson?ie=utf-8");
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefuc);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
		res = curl_easy_perform(curl);
		curl_easy_cleanup(curl);
		if( CURLE_OK != res )
		{
			printf("-----faied get curl------\n");
		}
		else
		{
			pstr = strstr(buffer, "{");
			buffer[ strlen(buffer) - 1 ] = '\0';
			jsonstr = json_tokener_parse(pstr);
			if(jsonstr)
			{
				json_object_object_get_ex(jsonstr, "cname", &item);
				if(item)
				{
					strcpy(procvince, json_object_get_string(item));
					json_object_put(item);
				}
				json_object_put(jsonstr);
			}
		}
	}
	
}
上一篇:数栈技术分享:利用 Atomic 构建 React 项目工作流,so easy!


下一篇:用两个开关控制一盏灯,简直so easy !