【发布时间】:2021-05-20 00:39:21
【问题描述】:
我正在使用带有 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");
}
}
编辑
this answer 中发布的代码有效,GetValue() 方法除外:
s1 = MyjsonStock->GetValue<UnicodeString>(_D("fin_id"));
这会在ustring.h 文件中给出错误:
'UnicodeString' 不引用值
此错误跳转到ustring.h 中UnicodeString 类的第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 c++builder