在C++Builder中解析嵌套的JSON文件



我使用的是带有64位编译器的C++Builder 10.4.2。我试图解析下面的嵌套JSON文件,使"US.NYSE"值位于TJSONArray中,然后循环它们以获得每个数组值。我的代码没有将"US.NYSE"放入数组中。你能展示如何将"US.NYSE"放入TJSONArray中吗?目前,我无法测试for循环,这个for循环设置正确吗?

{
"data":{
"US.NYSE":{
"fin_id":"US.NYSE",
"exchange":"New York Stock Exchange",
"market":"Canonical",
"products":null,
"local_time":"2020-11-27T11:55:00-05:00",
"status":"Open",
"reason":"Market Holiday - Primary Trading Session (Partial)",
"until":"2020-11-27T12:45:00-05:00",
"next_bell":"2020-11-27T13:00:00-05:00"
}
}
}
void __fastcall TForm1::Button33Click(TObject *Sender)
{
UnicodeString s1, s2, s3, s4, s5, s6, s7, s8, s9;
std::unique_ptr<TStringStream> jsonstream(new TStringStream);
jsonstream->LoadFromFile("marketstatus1.json");
TJSONObject* MyjsonFile = (TJSONObject*)TJSONObject::ParseJSONValue(jsonstream->DataString);
Application->MessageBoxW(MyjsonFile->ToString().w_str(), L"", 0);
TJSONArray* MyjsonArray = (TJSONArray*)TJSONObject::ParseJSONValue(MyjsonFile->GetValue("data")->ToString());
Application->MessageBoxW(MyjsonArray->ToString().w_str(), L"", 0);

int TestCount = MyjsonArray->Count;
for(int i=0; i<MyjsonArray->Count; i++){
s1 = MyjsonArray->Items[i]->GetValue<UnicodeString>("fin_id");
s2 = MyjsonArray->Items[i]->GetValue<UnicodeString>("exchange");
s3 = MyjsonArray->Items[i]->GetValue<UnicodeString>("market");
s4 = MyjsonArray->Items[i]->GetValue<UnicodeString>("products");
s5 = MyjsonArray->Items[i]->GetValue<UnicodeString>("local_time");
s6 = MyjsonArray->Items[i]->GetValue<UnicodeString>("status");
s7 = MyjsonArray->Items[i]->GetValue<UnicodeString>("reason");
s8 = MyjsonArray->Items[i]->GetValue<UnicodeString>("until");
s9 = MyjsonArray->Items[i]->GetValue<UnicodeString>("next_bell");
}
}

编辑

这个答案中发布的代码有效,除了GetValue()方法:

s1 = MyjsonStock->GetValue<UnicodeString>(_D("fin_id"));

这在ustring.h文件中给出错误:

"UnicodeString"不引用值

此错误跳到ustring.hUnicodeString类的第28行。我添加了这个文件的顶部,显示了下面的第28行。你能建议更改GetValue()方法的调用吗?

namespace System
{
class                  TVarRec;
class RTL_DELPHIRETURN Currency;
#if !defined(_DELPHI_NEXTGEN)
class RTL_DELPHIRETURN WideString;
#endif

/////////////////////////////////////////////////////////////////////////////
// UnicodeString: String class compatible with Delphi's Native 'string' type
/////////////////////////////////////////////////////////////////////////////
class RTL_DELPHIRETURN UnicodeString  //ERROR JUMPS HERE LINE(28)
{
friend UnicodeString operator +(const char*, const UnicodeString& rhs);
friend UnicodeString operator +(const wchar_t*, const UnicodeString& rhs);
friend UnicodeString operator +(const char16_t*, const UnicodeString& rhs);
friend UnicodeString operator +(const char32_t*, const UnicodeString& rhs);
public:

您对JSON的工作方式有误解。我建议您查看JSON标准定义的语法,该标准可在https://www.json.org.

您所显示的JSON中没有数组。阵列用[]括号表示。对象用{}大括号表示。因此,顶级JSON值是Object,"data“值是Object和"US.NYSE"是Object。因此,2个ParseJSONValue()调用都将返回TJSONObject,而不是TJSONArray

不需要第二次调用ParseJSONValue(),因为那些内部TJSONObject已经被第一次ParseJSONValue()调用解析,并且可以在MyjsonFile的值层次结构内部访问。访问这些值时,只需将返回值GetValue()键入TJSONObject*即可。

此外,您还需要deleteParseJSONValue()返回的值,以避免内存泄漏。

试试这个:

void __fastcall TForm1::Button33Click(TObject *Sender)
{
UnicodeString s1, s2, s3, s4, s5, s6, s7, s8, s9;
std::unique_ptr<TStringStream> jsonstream(new TStringStream);
jsonstream->LoadFromFile(_D("marketstatus1.json"));
std::unique_ptr<TJSONValue> MyjsonValue = TJSONObject::ParseJSONValue(jsonstream->DataString);
TJSONObject* MyjsonFile = static_cast<TJSONObject*>(MyjsonValue.get());
Application->MessageBoxW(MyjsonFile->ToString().c_str(), _D(""), 0);
TJSONObject* MyjsonData = static_cast<TJSONObject*>(MyjsonFile->GetValue(_D("data")));
Application->MessageBoxW(MyjsonData->ToString().c_str(), _D(""), 0);
TJSONObject* MyjsonStock = static_cast<TJSONObject*>(MyjsonData->GetValue(_D("US.NYSE")));
s1 = MyjsonStock->GetValue(_D("fin_id"))->Value();
s2 = MyjsonStock->GetValue(_D("exchange"))->Value();
s3 = MyjsonStock->GetValue(_D("market"))->Value();
s4 = MyjsonStock->GetValue(_D("products"))->Value();
s5 = MyjsonStock->GetValue(_D("local_time"))->Value();
s6 = MyjsonStock->GetValue(_D("status"))->Value();
s7 = MyjsonStock->GetValue(_D("reason"))->Value();
s8 = MyjsonStock->GetValue(_D("until"))->Value();
s9 = MyjsonStock->GetValue(_D("next_bell"))->Value();
}

最新更新