如何使用react-native-dropdown获取数据JSON?

时间:2018-03-19 04:29:59

标签: react-native dropdown

我想使用Dropdown lib从JSON获取数据, 但是我无法显示这些JSON。

这是我尝试过的代码:

this.state = {"diagnosis": {
        "type": [
          "Oncology",
          "Hip And Knee"
        ],
        "kode": [
          "123",
          "321",
          "3232",
          "1231"
        ],
        "PrimaryCat": [
          "contoh1",
          "contoh2",
          "contoh3"
        ],
        "Location": [
          "jakarta",
          "bogor",
          "depok",
          "tangerang",
          "bekasi"
        ],
        "Encountrace": [
          "kga",
          "tau",
          "isi",
          "menunya"
        ],
        "fracture": [
          "ini",
          "juga",
          "kaga",
          "tau",
          "isinya"
        ],
        "healing": [
          "yang",
          "pasti",
          "penyembuhan"
        ]
      }}

      render() {
    let data = [{
      value: 'Banana',
    }, {
      value: 'Mango',
    }, {
      value: 'Pear',
    }];
    return (
        <View>
          <Dropdown
            label="testing"
            data={this.state.diagnosis.type}
          />
        </View>
    );
  }
}

使用上面的代码,下拉列表只显示两行type,但oncologyhip and knee的名称不显示,

这是示例屏幕:

enter image description here

enter image description here

我做错了吗?

2 个答案:

答案 0 :(得分:1)

尝试以下。

{"diagnosis": {
  "type": [
    {
      value: "Oncology"
      },
    {
      value: "Hip And Knee
    }
  ],
  "kode": [
    {
      value: "123"
      },
    {
      value: "321"
      },
    {
      value: "3232"
      },
    {
      value: "1231
    }
  ],
  "PrimaryCat": [
    {
      value: "contoh1"
      },
    {
      value: "contoh2"
      },
    {
      value: "contoh3
    }
  ],
  "Location": [
    {
      value: "jakarta"
      },
    {
      value: "bogor"
      },
    {
      value: "depok"
      },
    {
      value: "tangerang"
      },
    {
      value: "bekasi
    }
  ],
  "Encountrace": [
    {
      value: "kga"
      },
    {
      value: "tau"
      },
    {
      value: "isi"
      },
    {
      value: "menunya
    }
  ],
  "fracture": [
    {
      value: "ini"
      },
    {
      value: "juga"
      },
    {
      value: "kaga"
      },
    {
      value: "tau"
      },
    {
      value: "isinya
    }
  ],
  "healing": [
    {
      value: "yang"
      },
    {
      value: "pasti"
      },
    {
      value: "penyembuhan
    }
  ]
}

答案 1 :(得分:1)

如果以下列格式更改json,

将会有效
this.state = {"diagnosis": {
  "type": [
    {
      value: "Oncology",
    }, {
      value: "Hip And Knee"
    }
  ],

其余格式如上所述。

如果您不想更改json的格式,则必须对react-native-material-dropdown代码进行细微更改,

请走这条路,

react-native-material-dropdown->src->components->dropdown->index.js

请在index.js中进行一些更改,以这种方式更改valueExtractor功能,

valueExtractor: ( value = {}, index) => value,

希望对你有所帮助。