我正在获取一些这样的数据:
2019-10-24 17:30:14.370 DEBUG 12024 --- [kground-preinit] .h.v.m.ResourceBundleMessageInterpolator : Loaded expression factory via original TCCL
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.0.RELEASE)
2019-10-24 17:30:14.573 INFO 12024 --- [ main] c.e.demomongoDB.DemoMongoDbApplication : Starting DemoMongoDbApplication on LAPTOP-QKODI51C with PID 12024 (started by HEMRAJ in D:\D\java projects eclipse\spring boot projects\spring boot CMS\demo-mongoDB)
2019-10-24 17:30:14.575 INFO 12024 --- [ main] c.e.demomongoDB.DemoMongoDbApplication : No active profile set, falling back to default profiles: default
2019-10-24 17:30:15.611 INFO 12024 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-10-24 17:30:15.636 INFO 12024 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 17ms. Found 0 repository interfaces.
2019-10-24 17:30:15.645 INFO 12024 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-10-24 17:30:15.648 INFO 12024 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 1ms. Found 0 repository interfaces.
2019-10-24 17:30:17.211 INFO 12024 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-10-24 17:30:17.226 INFO 12024 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-10-24 17:30:17.227 INFO 12024 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-10-24 17:30:17.377 INFO 12024 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-10-24 17:30:17.377 INFO 12024 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2689 ms
2019-10-24 17:30:17.866 INFO 12024 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-10-24 17:30:18.124 INFO 12024 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2019-10-24 17:30:18.397 WARN 12024 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.LocalDateTime to class java.time.Instant as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
2019-10-24 17:30:18.398 WARN 12024 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.Instant to class java.time.LocalDateTime as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
2019-10-24 17:30:18.432 WARN 12024 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.LocalDateTime to class java.time.Instant as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
2019-10-24 17:30:18.432 WARN 12024 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.Instant to class java.time.LocalDateTime as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
2019-10-24 17:30:18.653 INFO 12024 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-10-24 17:30:18.658 INFO 12024 --- [ main] c.e.demomongoDB.DemoMongoDbApplication : Started DemoMongoDbApplication in 4.974 seconds (JVM running for 5.844)
2019-10-24 17:30:22.338 INFO 12024 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.AsynchronousSocketChannelStream$OpenCompletionHandler.failed(AsynchronousSocketChannelStream.java:117) ~[mongodb-driver-core-3.11.1.jar:na]
at sun.nio.ch.Invoker.invokeUnchecked(Unknown Source) ~[na:1.8.0_211]
at sun.nio.ch.Invoker.invokeDirect(Unknown Source) ~[na:1.8.0_211]
at sun.nio.ch.Invoker.invoke(Unknown Source) ~[na:1.8.0_211]
at sun.nio.ch.Invoker.invoke(Unknown Source) ~[na:1.8.0_211]
at sun.nio.ch.WindowsAsynchronousSocketChannelImpl$ConnectTask.failed(Unknown Source) ~[na:1.8.0_211]
at sun.nio.ch.Iocp$EventHandlerTask.run(Unknown Source) ~[na:1.8.0_211]
at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(Unknown Source) ~[na:1.8.0_211]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) ~[na:1.8.0_211]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) ~[na:1.8.0_211]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_211]
Caused by: java.io.IOException: The remote computer refused the network connection.
at sun.nio.ch.Iocp.translateErrorToIOException(Unknown Source) ~[na:1.8.0_211]
at sun.nio.ch.Iocp.access$700(Unknown Source) ~[na:1.8.0_211]
... 5 common frames omitted
它给出一个输出:
@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.scss'],
encapsulation: ViewEncapsulation.None
})
数据看起来像JSON,但是,它的第一部分没有引号,因此我无法进行字符串化或JSON.parse。有没有办法访问这些数据?
答案 0 :(得分:0)
您已经使用const data = await res.json();
行将收到的数据解析为JavaScript对象,这就是为什么它没有引号的原因。
您也不用正确的属性访问数据,data["type"]
,data["name"]
和data["mal_id"]
不存在于对象的“根”中,而是存在于另一个对象中对象(mal_url
对象)中,因此您可以使用data.mal_url.type
,data.mal_url.name
和data.mal_url.mal_id
访问这些对象。
或使用方括号语法:data["mal_url"]["type"]
,data["mal_url"]["name"]
和data["mal_url"]["mal_id"]
。
因此固定代码如下:
async function fetchD() {
const res = await fetch("https://api.jikan.moe/v3/genre/anime/11");
const data = await res.json();
console.log(data.mal_url.type);
console.log(data.mal_url.name);
console.log(data.mal_url.mal_id);
}
fetchD();