⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test2.cpp

📁 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScript(Standard ECMA-262
💻 CPP
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern "C"
{

}

#include "json.h"
#pragma  comment(lib,"jsond.lib")

typedef struct {
	char *photo_id; 
	char *photo_title;
	char *photo_url;
	char *photo_file_url;
	char *longitude;
	char *latitude;
	char *width;
	char *height;
	char *upload_date; 
	char *owner_id;
	char *owner_url;
} panPhotoObject;

int get_panObj( struct json_object *photo_obj, panPhotoObject *thisPanObj );
int main(int argc, char **argv)
{
struct json_object *obj, *new_obj, *photoObj;
struct array_list *photoArray;
int count, i;
panPhotoObject *panObj;

new_obj = json_tokener_parse("{ \"count\": 2045898, \"photos\": [{\"photo_id\": 1578881, \"photo_title\": \"Rosina Lamberti,Sunset,Templestowe , Victoria, Australia\", \"photo_url\": \"http://www.panoramio.com/photo/1578881\", \"photo_file_url\": \"http://static2.bareka.com/photos/medium/1578881.jpg\", \"longitude\": 145.141754, \"latitude\": -37.766372, \"width\": 500, \"height\": 474, \"upload_date\": \"01 April 2007\", \"owner_id\": 140796, \"owner_name\": \"rosina lamberti\", \"owner_url\": \"http://www.panoramio.com/user/140796\"},{\"photo_id\": 901470, \"photo_title\": \"Holdf閚y鰈el閟 - Moonlight cuddle\", \"photo_url\": \"http://www.panoramio.com/photo/901470\", \"photo_file_url\": \"http://static3.bareka.com/photos/medium/901470.jpg\", \"longitude\": 15.970345, \"latitude\": 43.622159, \"width\": 500, \"height\": 361, \"upload_date\": \"19 February 2007\", \"owner_id\": 109117, \"owner_name\": \"Busa P閠er\", \"owner_url\": \"http://www.panoramio.com/user/109117\"}]}");
obj = json_object_object_get(new_obj, "count");
count = json_object_get_int(obj);
obj = json_object_object_get(new_obj, "photos"); 
photoArray = json_object_get_array(obj); 
for(i=0; i < photoArray->length; i++) 
{
	photoObj = json_object_array_get_idx(obj, i);
	panObj = (panPhotoObject*)malloc( sizeof( panPhotoObject ) );
	get_panObj( photoObj, panObj);
	free( panObj );
}
 

json_object_put(new_obj);
json_object_put(obj);
json_object_put(photoObj);

return 0;
}

int get_panObj( struct json_object *photo_obj, panPhotoObject *thisPanObj )
{
int retVal = 0;
struct json_object *obj;

	if ( !thisPanObj || !photo_obj )
		retVal = -1;
	else
	{
		obj = json_object_object_get(photo_obj, "photo_id");
		thisPanObj->photo_id = json_object_get_string(obj);
		/* Copy your value somewhere and free the json object */
		json_object_put(obj);

		obj = json_object_object_get(photo_obj, "photo_title");
		thisPanObj->photo_title = json_object_get_string(obj);

		obj = json_object_object_get(photo_obj, "photo_url");
		thisPanObj->photo_url = json_object_get_string(obj);

		obj = json_object_object_get(photo_obj, "photo_file_url");
		thisPanObj->photo_file_url = json_object_get_string(obj);

		obj = json_object_object_get(photo_obj, "longitude");
		thisPanObj->longitude = json_object_get_string(obj);

		obj = json_object_object_get(photo_obj, "latitude");
		thisPanObj->latitude = json_object_get_string(obj);

		obj = json_object_object_get(photo_obj, "width");
		thisPanObj->width = json_object_get_string(obj);

		obj = json_object_object_get(photo_obj, "height");
		thisPanObj->height = json_object_get_string(obj);

		obj = json_object_object_get(photo_obj, "upload_date");
		thisPanObj->upload_date = json_object_get_string(obj);

		obj = json_object_object_get(photo_obj, "owner_id");
		thisPanObj->owner_id = json_object_get_string(obj);

		obj = json_object_object_get(photo_obj, "owner_url");
		thisPanObj->owner_url = json_object_get_string(obj);
	}
return retVal;
}

int main2(int argc, char **argv)
{
  struct json_object *new_obj;

  mc_set_debug(1);

  new_obj = json_tokener_parse("/* more difficult test case */ { \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": [ { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"] } ] } } }");
  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  json_object_put(new_obj);

  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -