2017-08-14 98 views
0

我正在使用paho mqtt。我希望將數據發送到客戶端通過的TextInput JSON數組但似乎我越來越不確定的數據如何通過textinput數組以json格式發送數據

const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}); 

constructor(props) { 
super(props); 

this.state = { 
listofItems: [], 
    text: '', 
    text1: '', 
}} 


_addTask() { 
     const listofItems = [...this.state.listofItems, this.state.text, this.state.text1]; 

     this.setState({ 
      listofItems: listofItems, 
      text: '', 
      text1: '', 
     }); 

     this._changeTextInputValue(''); 
    } 

    _changeTextInputValue (text) { 
     this.setState({ 
      text 
     }); 
    } 

    _changeTextInputValue1 (text1) { 
     this.setState({ 
      text1 
     }); 
    } 

    static _renderRowData (rowData) { 
     return (
      <Text>{ rowData }</Text> 
     ) 
    } 

這是文字輸入

<TextInput autoCorrect={ false } 
    onChangeText={ (text) => this._changeTextInputValue(text) } 
    onSubmitEditing = {() => this.itemcodeDesc.focus()} 
    style={{backgroundColor:'#ddd'}} 
    value={ this.state.text } 
/> 

<TextInput 
autoCorrect={ false } 
onChangeText={ (text1) => this._changeTextInputValue1(text1) } 
onSubmitEditing={() => this._addTask() } 
returnKeyType={ 'done' } 
ref={(input) => this.itemcodeDesc = input} 
style={{backgroundColor:'#ddd'}} 
value={ this.state.text1 } 
/> 

而且按鈕:

<TouchableHighlight style={styles.button} onPress={this.sendMessage} underlayColor="transparent"> 
    <Text style={styles.buttonText}>NEXT</Text> 
</TouchableHighlight> 

,但我不能得到物品的JSON格式的數組作爲我稱之爲

"items": [{"itemcode":"'+this.state.listofItems[]+'"}] 

in sendMessage函數。有誰知道如何做到這一點?

+0

任何人都可以幫助我。我仍然遇到這個問題。提前謝謝你。 – kkumber

回答

0

我想它應該是this.state.listofItems而不是this.state.listofItems[]

希望這有助於

+0

謝謝你這一點,但輸出是這樣的: [{「項目」:「345,34,234,34」}] 是有可能得到這樣的: [{「項目」:「345 「,」itemdesc「:」34「},{」item「:」234「,」itemdesc「:」34「}] – kkumber

+0

在這種情況下,您的this.state.listofItems需要被格式化爲對象而不是數組,然後把這個作爲JSON.toString(this.state.listofItems)傳遞到相同的地方 – shinuq

+0

是我正在更改listofItems:[] listofItems:{} 我得到了這樣的輸出:[{「itemcode」:「[object JSON]「}] – kkumber