2017-08-14 6 views
0

Ich benutze Paho MQtt. Ich mag geschickt, um die Daten an das Client als JSON-Array über TextInput- aber es scheint, als ob ich nicht definierte DatenSo senden Sie Daten als JSON-Format über Texteingabefeld

immer bin
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> 
     ) 
    } 

Und dies ist die Texteingabe

<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 } 
/> 

Und die Taste:

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

aber ich kann das Array von Elementen im jSON-Format erhalten, wie ich diesen Anruf

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

in der sendMessage-Funktion. Weiß jemand, wie man das macht?

+0

Kann mir jemand helfen. Ich habe immer noch Probleme mit diesem. Vielen Dank im Voraus. – kkumber

Antwort

0

Ich denke, es ist wahrscheinlich this.state.listofItems anstatt this.state.listofItems[].

Hoffe, das hilft

+0

Vielen Dank dafür, aber der Ausgang geht so: [{ „Artikel“: „345,34,234,34“}] ist es möglich, so etwas zu bekommen: [{ „Artikel“: "345 "," itemdesc ":" 34 "}, {" item ":" 234 "," itemdesc ":" 34 "}] – kkumber

+0

In diesem Fall muss Ihr this.state.listofItems als Objekt formatiert werden, nicht als Array und dann übergeben Sie dies als JSON.toString (this.state.listofItems) an die gleiche Stelle – shinuq

+0

ist es richtig, dass ich listofItems ändern: [] to listofItems: {} Ich habe diese Ausgabe: [{"itemcode": "[Objekt JSON] "}] – kkumber