如何访问TMDB json数据中的数组元素



all!

我从TMDB请求一个JSON,并将其保存为本地硬盘上的文件。然后我读取该文件,并使用nlohmann::json对其进行解码。下一步是迭代json数据并提取信息的几个部分。虽然获取类型"string"、"boolean"等是没有问题的。但我很难处理"array"类型。(稍后类型"object"可能会出现同样的问题…)目标是将json数据转换为一些"ini"风格的类型,如

[tt1234567]
title = abcdefghij
runtime = 123
...

我通过以下方式迭代解码json的根:

using json = nlohmann::json;
{
auto jsonData = json::parse( jsonText );
// std::cout << jsonData.dump( 1 ) << "n";
for ( const auto &jsonItem : jsonData.items() )
{
jsonKey = jsonItem.key();
jsonValue = "";
if ( jsonItem.value().is_null() ) { jsonValue = "(null)"; }
else if ( jsonItem.value().is_boolean() ) { if ( jsonItem.value() ) { jsonValue = "(boolean) yes"; } else { jsonValue = "(boolean) no"; } }
else if ( jsonItem.value().is_string() ) { jsonValue = "(string) '" + string_left( jsonItem.value(), 25 ) + "'"; }
[ . . . ]
std::cout << jsonKey << ": " << jsonValue << "n";

屏幕输出类似于:

adult: (boolean) no
belongs_to_collection: (null)
budget: (unsigned) 45000000
credits: (object)
genres: (array) [ . . . ]
[ et al ]

我的问题是,我不知道处理"array"类型的正确语法,事实上,尽管它包含在[]中,但我不太确定它是否真的是一个数组

else if ( jsonItem.key() == "genres" ) // array
{
std::cout << "  jsonItem:       " << jsonItem << "n"; // {"genres":[{"id":12,"name":"Abenteuer"},{"id":28,"name":"Action"}]}
jsonKey = jsonItem.key();
std::cout << "  jsonKey:        " << jsonKey << "n"; // genres
// jsonValue = jsonItem.value(); // <-- returns array, but jsonValue expects string
// std::cout << "  jsonValue:      " << jsonValue << "n"; 
auto jsonValueArray = jsonItem.value().array();
std::cout << "  jsonValueArray: " << jsonValueArray << " (" << sizeof( jsonValueArray ) << ")n"; // [] (16)
auto jsonValueFlat = jsonItem.value().flatten();
std::cout << "  jsonValueFlat:  " << jsonValueFlat << "n"; // {"/0/id":12,"/0/name":"Abenteuer","/1/id":28,"/1/name":"Action"}
std::cout << "  " << jsonKey << " elements: " << jsonValueArray.size() << "n"; // 0
i = 0;
// for ( const auto &jsonValue : jsonValueArray )
// for ( i = jsonValueArray.begin(); i < jsonValueArray.end(); i++ )
for ( i = 0; i < jsonValueArray.size(); i++ )
{
std::cout << jsonValue << "n";
iniKey = "Genre" + std::to_string( i );
iniValue = "";
iniValue = jsonValue;
iniText.append( iniKey );
iniText.append( " = " );
iniText.append( iniValue );
iniText.append( "n" );
// i++;
}
}

产生

jsonItem:       {"genres":[{"id":12,"name":"Abenteuer"},{"id":28,"name":"Action"}]} 
jsonKey:        genres 
jsonValueArray: [] (16) 
jsonValueFlat:  {"/0/id":12,"/0/name":"Abenteuer","/1/id":28,"/1/name":"Action"} 
genres elements: 0

因此,我看到一个jsonItem的内容为"流派:[xxx]",因此它被识别为一个数组。"sizeof"返回16,我将其解释为4个指针,每个指针有4个字节(或2个指针有8个字节?)。另一方面,array()函数似乎返回一个空数组[],其中有0个元素。现在我陷入了…

我想要实现的是:从json中提取"流派"列表,并将元素与等";"连接起来

genres = Abenteuer;Action

在上面的例子中。

Michael

好吧,我想通了。中心点是JSON对象对.array()的误解。在摆弄了一点之后,下面的分支

else if ( jsonItem.key() == "genres" ) // array
{
std::cout << "  jsonItem:       " << jsonItem << "n"; // {"genres":[{"id":12,"name":"Abenteuer"},{"id":28,"name":"Action"}]}
jsonKey = jsonItem.key();
std::cout << "  jsonKey:        " << jsonKey << "n"; // genres
auto jsonValueArray = jsonItem.value(); // [{"id":12,"name":"Abenteuer"},{"id":28,"name":"Action"}] (16)
std::cout << "  jsonValueArray: " << jsonValueArray << " (" << sizeof( jsonValueArray ) << ")n"; // [] (16)
i = 0;
for ( const auto &jsonValue : jsonValueArray )
{
iniKey = "Genres";
iniValue = jsonValue["name"];
std::cout << "  " << jsonValue << "   -->   key:'" << iniKey << "', value:'" << iniValue << "'n";
if ( i == 0 )
{
iniText.append( iniKey );
iniText.append( " = " );
}
else
{
iniText.append( ";" );
}
iniText.append( iniValue );
i++;
}
iniText.append( "n" );
std::cout << "  genres added: " << std::to_string( i ) << "n";
}

现在生产

genres: (array) [ . . . ]
jsonItem:       {"genres":[{"id":12,"name":"Abenteuer"},{"id":28,"name":"Action"}]}
jsonKey:        genres
jsonValueArray: [{"id":12,"name":"Abenteuer"},{"id":28,"name":"Action"}] (16)
{"id":12,"name":"Abenteuer"}   -->   key:'Genres', value:'Abenteuer'
{"id":28,"name":"Action"}   -->   key:'Genres', value:'Action'
genres added: 2

分别。

[tt0446013]
adult = no
Genres = Abenteuer;Action
ID_TMDB = 1534
ID_IMDB = tt0446013
Title_Original = Pathfinder
Overview = An sich stammt der junge
Release_Date = 2007-01-11
Runtime = 99
Tagline = Zwei Welten , ein Krieger
Title = Pathfinder - Fährte des

这就是我想要的输出。

最新更新