假设我收到了一个请求URL = var url =“https://www.googleapis.com/books/v1/volumes?q=dart+java”;
在点击它时,我得到了回应,类似于下面提到的回复。如果我想简单列出这个标题列表,我怎么能在Dart中做到这一点?
我想在输出中显示每本书 标题 作者 出版者 发布日期 描述
{
"kind": "books#volumes",
"totalItems": 1025,
"items": [
{
"kind": "books#volume",
"id": "ECGJMAEACAAJ",
"etag": "jnz5pApZSnA",
"selfLink": "https://www.googleapis.com/books/v1/volumes/ECGJMAEACAAJ",
"volumeInfo": {
"title": "Dart in Action",
"authors": [
"Chris Buckett"
],
"publisher": "Manning Publications",
"publishedDate": "2013",
"description": "Summary Dart in Action introduces Google's Dart language and provides techniques and examples showing how to use it as a viable replacement for Java and JavaScript in browser-based desktop and mobile applications. It begins with a rapid overview of Dart language and tools, including features like interacting with the browser, optional typing, classes, libraries, and concurrency with isolates. After you master the core concepts, you'll move on to running Dart on the server and creating single page HTML5 web applications. About the Technology Dart is a web programming language developed by Google. It has modern OO features, just like Java or C#, while keeping JavaScript's dynamic and functional characteristics. Dart applications are \"transpiled\" to JavaScript, and they run natively in Dart-enabled browsers. With production-quality libraries and tools, Dart operates on both the client and the server for a consistent development process. About this Book Dart in Action introduces the Dart language and teaches you to use it in browser-based, desktop, and mobile applications. Not just a language tutorial, this book gets quickly into the nitty-gritty of using Dart. Most questions that pop up while you're reading are answered on the spot! OO newbies will appreciate the gentle pace in the early chapters. Later chapters take a test-first approach and encourage you to try Dart hands-on. To benefit from this book you'll need experience with HTML and JavaScript?a Java or C# background is helpful but not required. Purchase of the print book comes with an offer of a free PDF, ePub, and Kindle eBook from Manning. Also available is all code from the book. What's Inside Dart from the ground up Numerous code samples and diagrams Creating single-page web apps Transitioning from Java, C#, or JavaScript Running Dart in the browser and on the server About the Author Chris Buckett builds enterprise-scale web applications. He runs Dartwatch.com and is an active contributor to the dartlang list. \"Includes numerous examples of core language features as well as more advanced HTML5 features.\"—From the Foreword by Seth Ladd, Developer Advocate, Google Table of Contents PART 1 INTRODUCING DART Hello Dart \"Hello World\" with Dart tools Building and testing your own Dart app PART 2 CORE DART Functional first-class functions and closures Understanding libraries and privacy Constructing classes and interfaces Extending classes and interfaces Collections of richer classes Asynchronous programming with callbacks and futures PART 3 CLIENT-SIDE DART APPS Building a Dart web app Navigating offline data Communicating with other systems and languages PART 4 SERVER-SIDE DART Server interaction with files and HTTP Sending, syncing, and storing data Concurrency with isolates",
"industryIdentifiers": [
{
"type": "ISBN_10",
"identifier": "1617290866"
},
{
"type": "ISBN_13",
"identifier": "9781617290862"
}
],
"pageCount": 398,
"printType": "BOOK",
"categories": [
"Computers"
],
"averageRating": 3.0,
"ratingsCount": 1,
"contentVersion": "preview-1.0.0",
"imageLinks": {
"smallThumbnail": "http://bks9.books.google.co.in/books?id=ECGJMAEACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api",
"thumbnail": "http://bks9.books.google.co.in/books?id=ECGJMAEACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api"
},
答案 0 :(得分:1)
你可以这样做:
import 'dart:html';
import 'dart:convert';
HttpRequest.getString('https://www.googleapis.com/books/v1/volumes?q=dart+java')
.then((String fileContents) {
// parse the content with JSON and do what you want...
final json = JSON.decode(fileContents);
})
.catchError((Error error) {
print(error.toString());
});
答案 1 :(得分:0)
您可以像
一样处理它void main(List<String> x) {
var js = '''
... some JSON ...
''';
Map d = JSON.decode(js);
var books = (d['items'] as List).forEach((Map item) {
print('Title: ${item['volumeInfo']['title']}');
if(item['volumeInfo']['authors'] != null) {
(item['volumeInfo']['authors'] as List).forEach((a) => print(' Author: ${a}'));
}
});
}