React Native中的JSON对象渲染

时间:2019-04-03 17:13:36

标签: javascript json api react-native fetch

我想知道是否有可能创建一个调用group_id的节,并在react-native中列出该节中group_id中的所有成员

我尝试使用for循环,但无法正常工作。这是我目前正在使用的JSON对象。理想情况下,我希望列出group_ids以及该组中的成员。关于我应该如何开始的任何建议都很好。

{
  "groups": [
    {
      "group_id": 736,
      "members": [
        {
          "user_id": 1,
          "email": "jdoe@test.com",
          "first_name": "John",
          "last_name": "Doe"
        },
        {
          "user_id": 14,
          "email": "Hf@gmail.com",
          "first_name": "Be",
          "last_name": "Hg"
        },
        {
          "user_id": 10,
          "email": "Tea@gmail.com",
          "first_name": "Sugar",
          "last_name": "Tea"
        }
      ]
    },
    {
      "group_id": 737,
      "members": [
        {
          "user_id": 3,
          "email": "kentt@test.com",
          "first_name": "Kent",
          "last_name": "Torrel"
        },
        {
          "user_id": 12,
          "email": "Chh",
          "first_name": "Dog",
          "last_name": "Go"
        },
        {
          "user_id": 15,
          "email": "",
          "first_name": "",
          "last_name": ""
        }
      ]
    },
    {
      "group_id": 738,
      "members": [
        {
          "user_id": 6,
          "email": "rajpaj@test.com",
          "first_name": "Raj",
          "last_name": "Parikh"
        },
        {
          "user_id": 2,
          "email": "vt@test.com",
          "first_name": "Vathana",
          "last_name": "Him"
        },
        {
          "user_id": 11,
          "email": "Tea@gmail.com",
          "first_name": "Sugar",
          "last_name": "Tea"
        }
      ]
    },
    {
      "group_id": 739,
      "members": [
        {
          "user_id": 13,
          "email": "Hdh",
          "first_name": "Ou",
          "last_name": "Hm"
        },
        {
          "user_id": 9,
          "email": "lk@test.com",
          "first_name": "Larry",
          "last_name": "Jones"
        },
        {
          "user_id": 4,
          "email": "mcruz@test.com",
          "first_name": "Megan",
          "last_name": "Cruz"
        }
      ]
    },
    {
      "group_id": 740,
      "members": [
        {
          "user_id": 8,
          "email": "janejanej@test.com",
          "first_name": "Jajsdjao",
          "last_name": "Doe"
        },
        {
          "user_id": 16,
          "email": "",
          "first_name": "",
          "last_name": ""
        },
        {
          "user_id": 5,
          "email": "lbenjaminz@test.com",
          "first_name": "Liron",
          "last_name": "Benjamin"
        }
      ]
    }
  ]
}

这样的例子会有所帮助,因为我不知道从哪里开始!

1 个答案:

答案 0 :(得分:0)

您可以为每个循环使用

const data = {
            "groups": [
              {
                "group_id": 736,
                "members": [
                  {
                    "user_id": 1,
                    "email": "jdoe@test.com",
                    "first_name": "John",
                    "last_name": "Doe"
                  },
                  {
                    "user_id": 14,
                    "email": "Hf@gmail.com",
                    "first_name": "Be",
                    "last_name": "Hg"
                  },
                  {
                    "user_id": 10,
                    "email": "Tea@gmail.com",
                    "first_name": "Sugar",
                    "last_name": "Tea"
                  }
                ]
              },
              {
                "group_id": 737,
                "members": [
                  {
                    "user_id": 3,
                    "email": "kentt@test.com",
                    "first_name": "Kent",
                    "last_name": "Torrel"
                  },
                  {
                    "user_id": 12,
                    "email": "Chh",
                    "first_name": "Dog",
                    "last_name": "Go"
                  },
                  {
                    "user_id": 15,
                    "email": "",
                    "first_name": "",
                    "last_name": ""
                  }
                ]
              },
              {
                "group_id": 738,
                "members": [
                  {
                    "user_id": 6,
                    "email": "rajpaj@test.com",
                    "first_name": "Raj",
                    "last_name": "Parikh"
                  },
                  {
                    "user_id": 2,
                    "email": "vt@test.com",
                    "first_name": "Vathana",
                    "last_name": "Him"
                  },
                  {
                    "user_id": 11,
                    "email": "Tea@gmail.com",
                    "first_name": "Sugar",
                    "last_name": "Tea"
                  }
                ]
              },
              {
                "group_id": 739,
                "members": [
                  {
                    "user_id": 13,
                    "email": "Hdh",
                    "first_name": "Ou",
                    "last_name": "Hm"
                  },
                  {
                    "user_id": 9,
                    "email": "lk@test.com",
                    "first_name": "Larry",
                    "last_name": "Jones"
                  },
                  {
                    "user_id": 4,
                    "email": "mcruz@test.com",
                    "first_name": "Megan",
                    "last_name": "Cruz"
                  }
                ]
              },
              {
                "group_id": 740,
                "members": [
                  {
                    "user_id": 8,
                    "email": "janejanej@test.com",
                    "first_name": "Jajsdjao",
                    "last_name": "Doe"
                  },
                  {
                    "user_id": 16,
                    "email": "",
                    "first_name": "",
                    "last_name": ""
                  },
                  {
                    "user_id": 5,
                    "email": "lbenjaminz@test.com",
                    "first_name": "Liron",
                    "last_name": "Benjamin"
                  }
                ]
              }
            ]
          }

        let jsonData= JSON.stringify(data)
        jsonData = JSON.parse(jsonData)

        let groupID=740

        jsonData.groups.forEach(element => {
            if(element.group_id === groupID){
                console.log("Group ID "+element.group_id)

                element.members.forEach(subElement =>{
                    console.log(subElement)
                })
            }
        });