在反应原生中解析json时获取错误

时间:2017-03-15 13:51:10

标签: javascript android fetch react-native-android react-native-ios

这是我的json回复

    {
    "sessid": "some_value",
    "session_name": "some_vvalue",
    "token": "some_vvalue",
    "user": {
        "uid": "12125",
        "name": "some_vvalue",
        "mail": "some_vvalue",
        "theme": "",
        "signature": "",
        "signature_format": "filtered_html",
        "created": "1486476553",
        "access": "1489577945",
        "login": 1489585116,
        "status": "1",
        "timezone": "Asia/Kolkata",
        "language": "",
        "picture": "0",
        "data": false,
        "roles": {
            "2": "authenticated user"
        },
        "rdf_mapping": {
            "rdftype": ["sioc:UserAccount"],
            "name": {
                "predicates": ["foaf:name"]
            },
            "homepage": {
                "predicates": ["foaf:page"],
                "type": "rel"
            }
        }
    }
}

我只想获得前三个值,即session_name,session_id和token,并且我在发布请求期间获取此值,这是我的解析代码。

 async onPressLogin() {
    try {

      loaderHandler.showLoader("Please wait!!!");
      let response = await fetch(
        'http://some_url/api/user/login', {
          method: 'POST',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'X-CSRF-Token': this.state.token,
          },
          body: JSON.stringify({
            username: this.state.name,
            password: this.state.password,
          })
      });
      let responseText = await response.text();
      let responseJson = await response.json();

      if (response.status >= 200 && response.status < 300){


         await AsyncStorage.setItem(SESSION_TOKEN, responseJson.token);
         await AsyncStorage.setItem(SESSION_NAME,responseJson.session_name);
         await AsyncStorage.setItem(SESSION_ID,responseJson.sessid);

         Alert.alert('Store Token', 'Session Token'+responseJson.token)

      ///  Alert.alert('Server response', responseJson)
        loaderHandler.hideLoader();
        Actions.Documents();
      }
      else {
        loaderHandler.hideLoader();
        let error = responseText;

        Alert.alert('Login', error)
      }
    } catch(errors) {
      loaderHandler.hideLoader();
      Alert.alert('Login_server', errors)
    loaderHandler.hideLoader();
    }

   }

我正在尝试将这三个值存储在异步存储中以供进一步使用。请帮我尝试一切,但无法解决错误。如果您需要任何其他代码,请告诉我们。谢谢

这是我得到的错误     TypeError:预期的动态类型&#39;字符串&#39;但是类型为&#39; object&#39;

0 个答案:

没有答案