提取API从响应中获取原始值 [英] Fetch API get raw value from Response

查看:54
本文介绍了提取API从响应中获取原始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用React-Native请求一些数据,这是我的代码:

I use React-Native request some data, here is my code:

    fetch('https://raw.githubusercontent.com/facebook/react-native/master/docs/MoviesExample.json')
      .then((response)=>{
        return response.json()
      })
      .then((responseJSON)=>{
        callback(responseJSON)
      })
      .catch((error)=>{
        console.error(error);
      })
      .done()

我看到responseResponse对象,而json功能代码是return this.text().then(JSON.parse),我很困惑JSON.parse的参数是什么?那是response原始值吗?我怎么能得到它?

I see the response is a Response object, and the json function code is return this.text().then(JSON.parse), I'm confused that what is the parameter of theJSON.parse? Is that the response raw value? How can I get it?

推荐答案

以下是您要执行的操作.就我而言,我想手动解析JSON,因为内置JSON解析器不正确地解析了某个字符(\ u001e).

Here's how you'd do what you want. In my case I wanted to manually parse the JSON because a certain character (\u001e) is improperly parsed by the in-built JSON parser.

更改自:

fetch(url)
    .then(response => response.json()) 
    .then((data) => {
        data....

收件人:

fetch(url)
    .then(response => response.text()) 
    .then((dataStr) => {
        let data = JSON.parse(dataStr);
        data...

这篇关于提取API从响应中获取原始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆