2017-06-01 23 views
0

ここに私のコードです。React Native - JSONレスポンスからの解析エラー

私は次の関数を呼び出して状態リストを取得しています。

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)) 
     }); 
} 

次のエラーが発生しています。

enter image description here

答えて

0

警告のみの文字列が、あなたの場合 "stateArray" を示して複雑なオブジェクトである(配列、構造...)

ので(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'); 
}); 
+1

ありがとう、私は以下を探していた。 "stateArray.toString()またはJSON.stringify(stateArray)"を使用します。 –

関連する問題