【问题标题】:React Native - Parsing error from JSON ResponseReact Native - 来自 JSON 响应的解析错误
【发布时间】:2017-06-01 05:40:09
【问题描述】:

这是我的代码。

我正在调用以下函数来获取状态列表。

 callGetStatesApi()
 {
   callGetApi(GLOBAL.BASE_URL + GLOBAL.Get_States)
   .then((response) => {
           // Continue your code here...
           stateArray = result.data
           Alert.alert('Alert!', stateArray)
     });
 }

这是从 GET Api 获取响应的常用 callGetApi 函数。

export function callGetApi(urlStr, params) {
    return fetch(urlStr, {
            method: "GET",
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(params)
        })
        .then((response) => response.json())
        .then((responseData) => {
            result = responseData
        })
        .catch((error) => {
             console.error(error);
             Alert.alert('Alert Title failure' + JSON.stringify(error))
        });
} 

我收到以下错误。

【问题讨论】:

    标签: json reactjs parsing react-native


    【解决方案1】:

    Alert 仅显示字符串,但您的情况 "stateArray" 是复杂对象 (array,structure..)

    所以使用 stateArray.toString() 或 JSON.stringify(stateArray),

    或者

    试试下面的方法告诉我,

    fetch(GLOBAL.BASE_URL + GLOBAL.Get_States, {
        method: 'get',
        headers: { 'Accept': 'application/json','Content-Type': 'application/json',}
    }).then((response) => response.json())
    .then((responseData) => {
        console.log(responseData) // this is the response from the server
        // Continue your code here...
           stateArray = result.data
           Alert.alert('Alert!', stateArray)
    }).catch((error) => {
       console.log('Error');
    });
    

    【讨论】:

    • 谢谢,我正在寻找以下内容。 “使用 stateArray.toString() 或 JSON.stringify(stateArray)”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    相关资源
    最近更新 更多