微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JsonCpp

官网地址:

https://github.com/open-source-parsers/jsoncpp



一个简单测试:

void test_jsoncpp()
{
std::string jsoncppstring;


//写:

{
Json::StyledWriter writer;
Json::Value jmessage;


jmessage["test_strkey1"] = "value1";
jmessage["test_strkey2"] = "value2";
jmessage["test_strkey3"] = "value3";
jmessage["test_intkey1"] = 100;


std::string strjsonCpp = writer.write(jmessage); jsoncppstring = strjsonCpp;

OutputDebugStringA(strjsonCpp.c_str());

}


//读取:
{
Json::Reader reader;
Json::Value jmessage;

if (!reader.parse(jsoncppstring,jmessage))
{
RTC_LOG(WARNING) << "Received unkNown message. " << jsoncppstring;
return;
}

std::string type_str;
std::string json_object;

rtc::GetStringFromJsonObject(jmessage,"test_strkey1",&type_str);

int type_int;
bool b = rtc::GetIntFromJsonObject(jmessage,&type_int);

if (!b)
{
//return;
}


//遍历:
Json::Value::iterator itr = jmessage.begin();

for ( int n = 0; n<jmessage.size(); n++)
{
const Json::UInt i = itr.index();
const Json::Value jvalue = itr.key();

std::string str = itr.memberName();





const bool b1 = jvalue.isBool();
const bool b2 = jvalue.isstring();
const bool b3 = jvalue.isArray();

if (b1 == true)
{
bool b = jvalue.asBool();
}

if (b2 == true)
{
std::string str = jvalue.asstring()
}

if (b3 == true)
{

}


if (i)

{

}


itr++;
}

}





//获取key value:

for ( int n = 0; n<vectorStrMember.size(); n++)
{
std::cout<< vectorStrMember.at(n) << std::endl;
}



return;

}



转一遍文章

Jsoncpp的使用

from:https://blog.csdn.net/luxiaoyu_sdc/article/details/9276705


JSON建构于两种结构:

  • 名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。
  • 值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。

原文地址:https://www.jb51.cc/json/288579.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐