gpt4 book ai didi

c++ - 将字符串转换为 const *json 后,在传递 json 对象时,显示失败 : (IsObject()), 如何解决?

转载 作者:行者123 更新时间:2023-11-28 01:42:09 24 4
gpt4 key购买 nike

string strjson转换为const char* json后,interate时显示failed: (IsObject()), function FindMember,failed, 我不明白为什么显示这个,我认为这是 json 对象格式正确。

//
// main.cpp
// rapid
//
// Created by Shi Yan on 10/7/17.
// Copyright © 2017 Shi Yan. All rights reserved.
//

#include <iostream>
#include "rapidjson.h"
#include "document.h"
#include <fstream>
using namespace std;
using namespace rapidjson;
void readjson(){
ifstream handle("meta_Books.json");
if(handle.is_open()){
//cout<<"open success"<<endl;
const char* json;
string strjson;
int i=1;
while(getline(handle,strjson)){
if(i>4)
break;
cout<<strjson<<endl;
cout<<strjson.length()<<endl;
i++;
json=strjson.c_str();
cout<<"*********************"<<endl;
cout<<*json<<endl;
StringStream s (json);
Document document;
document.ParseStream(s);
Value::ConstMemberIterator itr = document.FindMember("asin");
cout<<itr->name.GetString()<<" = "<< itr->value.GetString()<<endl;
}
}
}
int main() {
readjson();
return 0;
}

我想的是json对象的格式,为什么失败了?

image

如您所见,getline() 方法效果很好,因为 string 的输出是一个完整的字符串

image

最佳答案

断言错误意味着 FindMember() 是在一个不代表 JSON 对象的 Value 上调用的(IsObject() 是错误)。

由于您显示的代码中只有 1 个 FindMember(),这意味着当 document.FindMember() 时 document.IsObject() 为 false 失败。您正在解析的 JSON 不是以其根中的对象开头,或者解析失败。您在代码中测试的两种情况都不是。

如果我不得不猜测(请不要让人们猜测!),失败的 JSON 文档可能包含未编码的换行符(在 JSON 字符串值中这不是非法的)。这会导致 std::getline() 过早退出,从而导致解析问题。 您显示的第一个屏幕截图支持该结论,表明 strjson 在发生错误时被拆分为 2 个单独的“行”。

我建议您尝试使用 RapidJSON 的 BasicIStreamWrapper,而不是使用 std::getline() 逐行读取文件,冒着嵌入式换行符错误的风险类来逐个文档地读取文件。 ParseStream() 有一个 kParseStopWhenDoneFlag 标志,允许从单个输入流解析多个根文档:

kParseStopWhenDoneFlag 

After parsing a complete JSON root from stream, stop further processing the rest of stream. When this flag is used, parser will not generate kParseErrorDocumentRootNotSingular error.

例如:

#include <iostream>
#include <fstream>
#include "rapidjson.h"
#include "document.h"
#include "istreamwrapper.h"

using namespace std;
using namespace rapidjson;

void readjson()
{
ifstream handle("meta_Books.json");
if (!handle.is_open())
{
// handle error...
cout << "error opening file" << endl;
}
else
{
BasicIStreamWrapper<ifstream> s(handle);
for(int i = 1; i <= 4; ++i)
{
Document document;
ParseResult pr = document.ParseStream<kParseStopWhenDoneFlag>(s);
if (!pr)
{
// handle error...
cout << "error parsing document " << i << endl;
}
else if (!document.IsObject())
{
cout << "document " << i << " is not an object" << endl;
}
else
{
Value::ConstMemberIterator itr = document.FindMember("asin");
if (itr != document.MemberEnd())
cout << "asin = " << itr->value.GetString() << endl;
else
cout << "asin not found" << endl;
}
}
}
}

int main()
{
readjson();
return 0;
}

关于c++ - 将字符串转换为 const *json 后,在传递 json 对象时,显示失败 : (IsObject()), 如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46719567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com