React Native类型NSDictionary的JSON值无法转换为NSString

问题描述:

我试图开发一个React Native(版本0.38.0)应用程序的问题。我基本上是在学习。React Native类型NSDictionary的JSON值无法转换为NSString

我从自定义API端点获取JSON。我已设法循环浏览JSON并输出一些数据,但问题在于链接到图像的string。我不断收到错误:

JSON Value of { robj = "https://example.com/img.jpg" } type NSDictionary Cannot be converted to NSString. 

在谷歌周围有一个侦察员,我看不出这个问题的头部和尾部。

任何人都可以请指点我正确的方向吗?

var api = { 
    getData() { 
    var url = 'https://example.com/api/json/' 
    return fetch(url).then((res) => res.json()) 
    } 
} 

class GetCategories extends Component { 
    constructor(props) { 
    super(props) 
    this.state = { 
     categories: [] 
    } 
    } 

    componentWillMount() { 
    api.getData().then((res) => { 
     this.setState({ 
     categories: res 
     }) 
    }) 
    } 

    render() { 
    var catLoop =this.state.categories.map(function(obj, index){ 
     var catTitle = '', 
      catImage = ''; 

     catTitle = obj.title; 
     catImage = obj.image; 

     return (
     <View key={index}> 
      <Image source={{uri: {catImage}}} style={{width: 400, height: 400}}> 
      <Text>{catTitle}</Text> 
      </Image> 
     </View> 
    ) 

    }); 

    return <View>{catLoop}</View> 
    } 
} 
+0

你能提供的代码您有返回这个错误?特别是''源代码分配。 –

+0

@BradBumbalough查看已更新的代码问题。赞赏。 –

+0

我解决它通过添加 {catTitle} 但图像惯于显示。 –

这应该解决您的问题...

<Image source={{uri: catImage}} style={{width: 400, height: 400}}> 
    <Text>{catTitle}</Text> 
</Image> 
+0

事先在评论中解决。不管怎么说,还是要谢谢你。 –