有一個我解碼為 JSON 的 URL 地址,還有一個我正在嘗試將其轉換為物件串列(書)的特定標題(結果)。
當我列印此標題的內容時,我確實得到了所需的結果(第一次列印),但是當我創建串列并嘗試列印串列中第一個物件的一個??欄位時,沒有列印任何內容(第二次列印)。
這意味著我創建串列的方式有問題,我找不到它。
我將衷心感謝您的幫助。
JSON結構:
{count:9447,結果:[{book_about:string,book_label:string,book_wikilink:string,片段:[string1,string2,string3,string4],id:string,label:string,text:string]}
它的圖片:https ://i.stack.imgur.com/w2JGm.png
我的代碼:
Future<List<Book>> fetchBooks(http.Client client) async {
var url = 'http://jbse.cs.technion.ac.il:3030/search?query=?????&start=0';
final response = await http.get(Uri.parse(url),headers: {'Content-Type': 'application/json'});
Map<String, dynamic> responseBody = jsonDecode(response.body);
dynamic results = responseBody['result'];
print(results);
List<Book> list = results.map((book) => Book.fromJson(book)).toList();
print(list.first.book_wikilink);
return list;
}
class Book {
String book_about;
String book_label;
String book_wikilink;
List<String> fragments;
String id;
String label;
String text;
Book(
this.book_about,
this.book_label,
this.book_wikilink,
this.fragments,
this.id,
this.label,
this.text
);
factory Book.fromJson(Map<String, dynamic> json) {
return Book(
json['book_about'],
json['book_label'],
json['book_wikilink'] ,
json['fragments'],
json['id'],
json['label'],
json['text'],
);
}
}
uj5u.com熱心網友回復:
您可以手動完成。
即時選擇并轉換您想要的內容(在決議期間)。
import 'package:fast_json/fast_json_selector.dart' as parser;
import 'package:http/http.dart' as http;
void main(List<String> args) async {
final books = await fetchBooks();
print('Found: ${books.length} book(s).');
print(books.map((e) => e.label).join('\n'));
}
Future<List<Book>> fetchBooks() async {
var url = 'http://jbse.cs.technion.ac.il:3030/search?query=?????&start=0';
final response = await http
.get(Uri.parse(url), headers: {'Content-Type': 'application/json'});
return _findBooks(response.body);
}
List<Book> _findBooks(String source) {
// Path to Book
// Map => property "result" => List => list index => Map (Book)
final level = '{} result [] 0 {}'.split(' ').length;
final books = <Book>[];
void select(parser.JsonSelectorEvent event) {
if (event.levels.length == level) {
final book = Book.fromJson(event.lastValue as Map);
books.add(book);
// Freeing the memory allocated for the book
// because it is no longer needed
event.lastValue = null;
}
}
parser.parse(source, select: select);
return books;
}
class Book {
String book_about;
String book_label;
String book_wikilink;
List<String> fragments;
String id;
String label;
String text;
Book(this.book_about, this.book_label, this.book_wikilink, this.fragments,
this.id, this.label, this.text);
factory Book.fromJson(Map json) {
return Book(
json['book_about'] as String,
json['book_label'] as String,
json['book_wikilink'] as String,
(json['fragments'] as List).cast(),
json['id'] as String,
json['label'] as String,
json['text'] as String,
);
}
}
輸出:
Found: 10 book(s).
????? ??? ???? ??
???? ??? ?????? ?? ??
??"? ?????? ?? ??
??? ????? ?????? ?? ??
??? ??? ???? ?????? ?? ??
?????? ?????? ?? ??
??"? ?????? ?? ??
???? ????? ?????? ?? ??
???? ??? ?????? ?? ??
???"? ?????? ?? ??
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/475435.html
