如何解决JSON解析错误:React Native中出现意外的标识符“对象”?

问题描述

我使用Axios从rss2json api中获取。我使用Hooks将来自Axios的response.data解析为提要。现在,我想解析该提要到react-native-deck-swiper swiper组件中,但是出现错误“如何解决JSON解析错误:React Native中意外的标识符“ object”?”。 我该如何解决?

  dynamic NEWPINCO;
  addAddressPersistent(dynamic value) async {
    NEWPINCO = '';
    print("VALUE PASSED FOR PINCODE : $value");
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString('NEWPINCO',value); //setting it's a Future
    setState(() {
      NEWPINCO = prefs.getString('NEWPINCO');
      print("$NEWPINCO is the number");
    });
  }

库:react-native-deck-swiper,axios 我使用rss2json.com将rss供稿转换为json,然后使用Axios进行获取。

编辑:

enter image description here

enter image description here

enter image description here

解决方法

我们以前也遇到过这个问题。即使数据是正确的json,它有时也会以字符串形式返回数据。我们正在做这样的事情

axios.get(URL)
.then(response => {
  let responseData = response.data
  const responseJson = (typeof responseData == "string" ? JSON.parse(responseData.trim()) : responseData);
  setFeed(responseJson)
})

您可以尝试像这样加载刷卡器吗?

<View>
  {
      feed && <Swiper 
            cards={feed}
            cardIndex={index}
            renderCard={(card) => {
                 <View><Image source={{uri: feed.items[0].thumbnail}} /></View>
                }}
        ></Swiper>
  }
  </View>
,

尝试这种方式;)

useEffect(() => {
    axios.get(URL)
       .then(response => {
          // items ==> [{thumbnail: "",... },{thumbnail: "",... }]
          const { items } = response.data
          setFeed(items)
    })
},[URL])


        
return (
    <View style={styles.container}>
        {
          feed && <Swiper 
            cards={feed}
            cardIndex={index}
            renderCard={(card) => {
                <View style={styles.card}>
                  <Image 
                    style={styles.cardImage} 
                    source={{uri: card.thumbnail}} />
                </View>
            }}
        ></Swiper>
      }
    </View>
  );

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...