How to save part of JSON message

时间:2018-09-22 22:49:46

标签: android json

I have a problem, because my application has connected to BLE device, and my device is sending message to application with part JSON.

For example:

<1;json part 1;checksum> ,<1:json part 2; checksum> 

And etc.

I don't know how I can save and join this part. I thought, that I should to use Room library for this, but this is not good idea surely.

1 个答案:

答案 0 :(得分:0)

我认为您要完成的工作是构造一个JSONObject。 JSON对象可帮助您将数据存储为单个对象,并为每个对象分配键以便访问它们。 JSON也被称为是客户端和服务器以压缩方式更轻松地交谈和传输数据的常见语言之一。

以下是在Java中使用JSON对象的示例:

export default class DataView extends React.Component {

  constructor(props) {
    super(props);
    this.state = {message: 'executing...', body: '', headers: '', 'url': ''}
  }

  componentWillMount() {
      ajax.post(this.state.url, this.state.body, this.state.headers)
          .subscribe(
            result => {
              this.setState({ message: "code " + result.status });
              console.log(result.status);
            },
            error => {
              this.setState({ message: "Problem: " + error.status });
              console.log("Problem: " + error.status)
           });
  }

  render() {
    return (
      <div style={divStyle}>
        Message: {this.state.message}
      </div>);
  }
}

这将导致以下JSON:

JSONObject jsonObject = new JSONObject();
jsonObject.put("1", "A sample string");
jsonObject.put("2", 1234);
jsonObject.put("AnEmptyObject", new JSONObject());
jsonObject.put("AnEmptyArray", new JSONArray());