首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在React Native中访问数组中的多个JSON响应?

如何在React Native中访问数组中的多个JSON响应?
EN

Stack Overflow用户
提问于 2018-10-19 07:43:43
回答 1查看 306关注 0票数 0

通常,我会在React Native中使用reponseJson来访问JSON的响应,但是如何访问多个响应呢?

我把它从php json_encoded这里:

Login.php

代码语言:javascript
复制
if ($row = mysqli_fetch_array($result)) {
     $allresults = array( 'Data Matched' => $objA, "Hello" => $objB );
        $Success = 'Data Matched';
     // Converting the message into JSON format.
    $Success = json_encode($allresults);

    // Echo the message.
     echo $Success ; 
} else {}

当我试图通过responseJson.objA访问它时,我得到了这个错误:[object Object] in React Native:

代码语言:javascript
复制
    Login = () =>{
this.setState({ loading: true, disabled: true }, () =>{

fetch('http://www.exmaple.com/React/login.php', {
 method: 'POST',
 headers: {
   'Accept': 'application/json',
   'Content-Type': 'application/json',
 },
 body: JSON.stringify({

   username: username,

 })

}).then((response) => response.json())
     .then((responseJson) => {

       // If server response message same as Data Matched
      if(responseJson.objA === 'Data Matched')**<-- Want the first response to go here** 
       {
            this.dropdown.alertWithType('success', 'Login Success', reponseJson.objB); **<-- Want the second response to go here**
        }
       }).catch((error) => {
      console.log(error);
     });

我从SO,我做错了什么?multiple JSON object response跟随这个问题。

EN

回答 1

Stack Overflow用户

发布于 2018-10-19 07:56:07

您可能希望将此代码放在react本机代码中的if语句之前,

代码语言:javascript
复制
alert(JSON.stringify(responseJson, null, 1));

所以你可以看到字符串形式的响应。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52884004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档