我有一个tweeter json(保存在一个名为latest.json的文件中),我解析为"jasonvalue doc",我尝试读取它并打印特定数据.下面的代码每次打印相同的数据,而不是数据形成整个json.
auto content = to!string(read("latest.json"));
JSONValue doc = parseJSON(content).object;
while (i<3){
writeln(doc.object["created_at"].str,"\n");
writeln(doc.object["text"].str,"\n");
writeln(doc.object["retweet_count"].integer,"\n");
i++;
}
Run Code Online (Sandbox Code Playgroud)
我怎么能读到所有的杰森?
你应该循环遍历项目数组.从twitter获取json,就像这里的例子:https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline它是一个对象数组.
那样做
JSONValue[] doc = parseJSON(content).array;
foreach(tweet; doc) {
writeln(tweet.object["text"].str);
// and other info
}
Run Code Online (Sandbox Code Playgroud)
应该修复它.