cocos2d-x解析json文件(C++)

bool MyLottery::LoadJson(){


	m_lotteryItemMap.clear(); 
	rapidjson::Document doc;
	std::string data = FileUtils::getInstance()->getStringFromFile("lottery.json");
	doc.Parse(data.c_str());
	if (doc.HasParseError())
		return false;

	rapidjson::Value &LotteryId = doc["LotteryId"];

	rapidjson::Value &lotteryItemInfo = doc["LotteryItems"];
	if (!lotteryItemInfo.Empty())
	{
		size_t LotteryItemCount = lotteryItemInfo.Size();
		for (size_t i = 0; i < LotteryItemCount; ++i)
		{
			rapidjson::Value &lotteryItemValue = lotteryItemInfo[i];
			LotteryItem lottery;
			if (lotteryItemValue.HasMember("ItemId"))
				lottery.ItemId = lotteryItemValue["ItemId"].GetInt();
			else
				continue;
			if (lotteryItemValue.HasMember("LotteryItemId"))
				lottery.LotteryItemId = lotteryItemValue["LotteryItemId"].GetInt();
			if (lotteryItemValue.HasMember("ItemName"))
				lottery.ItemName = lotteryItemValue["ItemName"].GetString();
			if (lotteryItemValue.HasMember("ItemCount"))
				lottery.ItemCount = lotteryItemValue["ItemCount"].GetInt();
			if (lotteryItemValue.HasMember("ImagePath"))
				lottery.ImagePath = lotteryItemValue["ImagePath"].GetString();
			if (lotteryItemValue.HasMember("ItemType"))
				lottery.ItemType = lotteryItemValue["ItemType"].GetInt();
			m_lotteryItemMap[lottery.ItemId] = lottery;
		}
	}


}
{
    "LotteryId": 22,
    "NotHitItem": 186,
    "LotteryItemsImg": "lottery/lottery_items.png",
    "RuleImg": "lottery/rule_img.png",
    "Opportunities": "剩余1次",
    "LotteryItems": [
        {
            "ItemId": 21,
            "ItemType": 1,
            "LotteryItemId": 187,
            "ItemName": "1000金蛋",
            "ItemCount": 1000,
            "ImagePath": "coin_lottery.png"
        },
        {
            "ItemId": 22,
            "ItemType": 0,
            "LotteryItemId": 186,
            "ItemName": "谢谢参与",
            "ItemCount": 0,
            "ImagePath": "back_btn.png"
        },
        {
            "ItemId": 23,
            "ItemType": 2,
            "LotteryItemId": 192,
            "ItemName": "200元话费",
            "AwardId": 30,
            "ItemCount": 18,
            "ImagePath": "phone_lottery.png"
        },
        {
            "ItemId": 24,
            "ItemType": 1,
            "LotteryItemId": 188,
            "ItemName": "10金蛋",
            "ItemCount": 2000,
            "ImagePath": "coin_lottery.png"
        },
        {
            "ItemId": 25,
            "ItemType": 1,
            "LotteryItemId": 191,
            "ItemName": "5元话费",
            "ItemCount": 100,
            "ImagePath": "phone_lottery.png"
        },
        {
            "ItemId": 26,
            "ItemType": 1,
            "LotteryItemId": 189,
            "ItemName": "200金蛋",
            "ItemCount": 5000,
            "ImagePath": "coin_lottery.png"
        },
        {
            "ItemId": 27,
            "ItemType": 1,
            "LotteryItemId": 190,
            "ItemName": "1元话费",
            "ItemCount": 50,
            "ImagePath": "phone_lottery.png"
        },
        {
            "ItemId": 28,
            "ItemType": 2,
            "LotteryItemId": 194,
            "ItemName": "100奖券",
            "ItemCount": 1,
	    "ImagePath": "prop2.png"
        }
    ]
}
json文件的格式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值