2024年最全rapidjson安装学习_rapidjson anzhuan(1),2024年最新C C++中为什么需要Handler

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

}


### 在这里插入图片描述


demo2:  
 针对如下的json串:  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191110230100219.png)  
 dom树实现:  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191110230133312.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM0NTcxNjc=,size_16,color_FFFFFF,t_70)  
 代码:



// Hello World example
// This example shows basic usage of DOM-style API.

#include “rapidjson/document.h” // rapidjson’s DOM-style API
#include “rapidjson/prettywriter.h” // for stringify JSON
#include

using namespace rapidjson;
using namespace std;

int main(int, char*[]) {

// 1. Parse a JSON text string to a document.

const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ";
printf("Original JSON:\n %s\n", json);

Document document;  // Default template parameter uses UTF8 and MemoryPoolAllocator.

#if 0
// “normal” parsing, decode strings to new buffers. Can use other input stream via ParseStream().
if (document.Parse(json).HasParseError())
return 1;
#else
// In-situ parsing, decode strings directly in the source string. Source must be string.
char buffer[sizeof(json)];
memcpy(buffer, json, sizeof(json));
if (document.ParseInsitu(buffer).HasParseError())
return 1;
#endif

printf("\nParsing to document succeeded.\n");


// 2. Access values in document. 

printf("\nAccess values in document:\n");
assert(document.IsObject());    // Document is a JSON value represents the root of DOM. Root can be either an object or array.

assert(document.HasMember("hello"));
assert(document["hello"].IsString());
printf("hello = %s\n", document["hello"].GetString());

// Since version 0.2, you can use single lookup to check the existing of member and its value:
Value::MemberIterator hello = document.FindMember("hello");
assert(hello != document.MemberEnd());
assert(hello->value.IsString());
assert(strcmp("world", hello->value.GetString()) == 0);
(void)hello;

assert(document["t"].IsBool());     // JSON true/false are bool. Can also uses more specific function IsTrue().
printf("t = %s\n", document["t"].GetBool() ? "true" : "false");

assert(document["f"].IsBool());
printf("f = %s\n", document["f"].GetBool() ? "true" : "false");

printf("n = %s\n", document["n"].IsNull() ? "null" : "?");

assert(document["i"].IsNumber());   // Number is a JSON type, but C++ needs more specific type.
assert(document["i"].IsInt());      // In this case, IsUint()/IsInt64()/IsUint64() also return true.
printf("i = %d\n", document["i"].GetInt()); // Alternative (int)document["i"]

assert(document["pi"].IsNumber());
assert(document["pi"].IsDouble());
printf("pi = %g\n", document["pi"].GetDouble());

{
    const Value& a = document["a"]; // Using a reference for consecutive access is handy and faster.
    assert(a.IsArray());
    for (SizeType i = 0; i < a.Size(); i++) // rapidjson uses SizeType instead of size\_t.
        printf("a[%d] = %d\n", i, a[i].GetInt());
    
    int y = a[0].GetInt();
    (void)y;

    // Iterating array with iterators
    printf("a = ");
    for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)
        printf("%d ", itr->GetInt());
    printf("\n");
}

// Iterating object members
static const char\* kTypeNames[] = { "Null", "False", "True", "Object", "Array", "String", "Number" };
for (Value::ConstMemberIterator itr = document.MemberBegin(); itr != document.MemberEnd(); ++itr)
    printf("Type of member %s is %s\n", itr->name.GetString(), kTypeNames[itr->value.GetType()]);


// 3. Modify values in document.

// Change i to a bigger number
{
    uint64\_t f20 = 1;   // compute factorial of 20
    for (uint64\_t j = 1; j <= 20; j++)
        f20 \*= j;
    document["i"] = f20;    // Alternate form: document["i"].SetUint64(f20)
    assert(!document["i"].IsInt()); // No longer can be cast as int or uint.
}

// Adding values to array.
{
    Value& a = document["a"];   // This time we uses non-const reference.
    Document::AllocatorType& allocator = document.GetAllocator();
    for (int i = 5; i <= 10; i++)
        a.PushBack(i, allocator);   // May look a bit strange, allocator is needed for potentially realloc. We normally uses the document's.

    // Fluent API
    a.PushBack("Lua", allocator).PushBack("Mio", allocator);
}

// Making string values.

// This version of SetString() just store the pointer to the string.
// So it is for literal and string that exists within value's life-cycle.
{
    document["hello"] = "rapidjson";    // This will invoke strlen()
    // Faster version:
    // document["hello"].SetString("rapidjson", 9);
}

// This version of SetString() needs an allocator, which means it will allocate a new buffer and copy the the string into the buffer.
Value author;
{
    char buffer2[10];
    int len = sprintf(buffer2, "%s %s", "Milo", "Yip");  // synthetic example of dynamically created string.

    author.SetString(buffer2, static\_cast<SizeType>(len), document.GetAllocator());
    // Shorter but slower version:
    // document["hello"].SetString(buffer, document.GetAllocator());

    // Constructor version: 
    // Value author(buffer, len, document.GetAllocator());
    // Value author(buffer, document.GetAllocator());
    memset(buffer2, 0, sizeof(buffer2)); // For demonstration purpose.
}
// Variable 'buffer' is unusable now but 'author' has already made a copy.
document.AddMember("author", author, document.GetAllocator());

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值