简体   繁体   English

反应本地JSON响应

[英]React native json response

I am trying to print json responce in via console.log ,below is my json response but i want each element to print one by one like user.id or product.name 我正在尝试通过console.log打印json响应,以下是我的json响应,但我希望每个元素都像user.id或product.name一样逐一打印

    {
    "user": [{
        "id": "1",
        "name": "User 1"
    }, {
        "id": "2",
        "name": "User 2"
    }],
    "user_pictures": false,
    "products": [{
        "id": "1",
        "name": "test abc"
    }, {
        "id": "2",
        "name": "test abc 1"
    }],
    "purpose": ""
}

I am trying like this: 我正在这样尝试:

responseData.map((item)=>{
                console.log(item.user.id)
            })

but error is show in console 但是控制台中显示错误

TypeError: responseData.map is not a function

responseData is the response in json when i call my fetch method 当我调用fetch方法时,responseData是json中的响应

You can access id like this input.user[0].id . 您可以像此input.user[0].id那样访问id

Below is working code: 下面是工作代码:

 let input = { "user": [{ "id": "1", "name": "User 1" }, { "id": "2", "name": "User 2" }], "user_pictures": false, "products": [{ "id": "1", "name": "test abc" }, { "id": "2", "name": "test abc 1" }], "purpose": "" }; console.log(input.user[0].id); 

responseData is an object... you cant map over an object... responseData has 4 properties... users, user_pictures, products, purpose... of which, responseData.users and responseData.products are arrays that can be mapped over. responseData是一个对象...您无法映射到一个对象上... responseData具有4个属性... users,user_pictures,products,目的...其中, responseData.usersresponseData.products是可以映射到其上的数组。 user_pictures is a boolean and purpose is a string. user_pictures是一个布尔值,用途是一个字符串。

Its Works like this: 其工作方式如下:

JSON.parse(responseData).user.map((item)=>{
                console.log(item.id)
            })

Thank guys 谢谢你们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM