私が理解したように、この char* は有効な json-string です。
const char* json = { "array":[14, -15, 3.17], "array_type": ["uint", "int", "float"] }
配列内のすべての数値は 4 バイトです。
Rapidjson を使用して配列をループするにはどうすればよいでしょうか?
これまでの私のコードは次のとおりです。
#include "rapidjson/document.h"
using namespace rapidjson;
int main(void)
{
int i_val; unsigned int ui_val; float f_val;
const char* json = "{ \"array\":[14, -15, 3.17], \"array_type\" : [\"uint\", \"int\", \"float\"] }";
Document d;
d.Parse<0>(json);
Value& a = d["array"];
Value& at = d["array_type"];
for (SizeType i = 0; i<a.Size(); i++)
{
if (a[i].IsInt() && (std::string)at[i].GetString() == "int")
{
i_val = a[i].GetInt();
//Do anything
}
if (a[i].IsUint() && (std::string)at[i].GetString() == "uint")
{
ui_val = a[i].GetUint();
//Do anything
}
if (a[i].IsDouble() && (std::string)at[i].GetString() == "float")
{
f_val = (float)a[i].GetDouble();
//Do anything
}
}//end for
return 0;
}
エラー:
TestApp: /workspace/TestApp/src/include/rapidjson/document.h:1277: rapidjson::GenericValue<Encoding, Allocator>& rapidjson::GenericValue<Encoding, Allocator>::operator[](rapidjson::SizeType) [with Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>; rapidjson::SizeType = unsigned int]: Assertion `index < data_.a.size' failed.
GetDouble の後に実行すると、関数 a.Size() でコードがクラッシュします。どうすればこれを解決できますか?
最後の「if」が間違っていることはわかっています。それがおそらくプログラムがクラッシュする理由です。デフォルトでは、Double は 8 バイトで、SizeType は 4 バイトです。
配列をループする解決策はありますか?? そうでない場合は、他のライブラリでも問題ありません.これら3つの異なるタイプの値をjsonで転送する必要があります。ちなみに、配列の長さは 1 から 500 までです。
(GetFloat() 関数はありません。)
助けてくれてありがとう。よろしく