Rifat Murtuza
Rifat Murtuza

Reputation: 119

Json Parser error in React native

Hello I need a solution for multiple json post through react native since I'm new in React native. Here is my new json data I need to post

[
  {
    "rollno": "10",
    "typeofattendence": 1
  },
  {
    "rollno": "10021",
    "typeofattendence": 0
  }
]

Here is my fetch data please note I can post single json data not able to post multiple.Here is my code

body: JSON.stringify({
        rollno: this.state.data,
        typeofattendence: this.state.value
      })`      `body: JSON.stringify({
        rollno: this.state.data,
        typeofattendence: this.state.value
      })

Please help me . Here you can see I can post single json object but how i post inside array multiple object . Thanks in advance

Upvotes: 0

Views: 358

Answers (2)

DNA.h
DNA.h

Reputation: 863

You can use:

var myarray = [];
var myJSON = "";

var item = {
    "rollno": "10",
    "typeofattendence": 1
  };

myarray.push(item);

item = {
    "rollno": "10021",
    "typeofattendence": 0
  }
myarray.push(item);

myJSON = JSON.stringify({myarray: myarray});

As this http://jsfiddle.net/jensbits/MWSeg/ toturial says.

Upvotes: 0

slashsharp
slashsharp

Reputation: 2833

You should store your object in an array first. For example.

let data = [];
data.push({
 rollno: this.state.data,
 typeofattendence: this.state.value
});

and when you want to send it to the server

body: JSON.stringify(data);

Upvotes: 1

Related Questions