Special JSON object to the array, the most synthetic new JSON data

Source: Internet
Author: User

The JSON data obtained from the background today is as follows:

var datajson = {    ' 0 ': {id:1 }    , '    1 ': {id:2}, ' 2 ': {id:3 },    /c7> ' count ': 1}

This JSON we take the data very troublesome, datajson[' 0 '] to get to the first data, and now this JSON does not have the length of this property, so, can not be like the array over there to traverse the output processing.

Generally from the background to get the data should be the following, only to facilitate the JS access and processing data

var json = {  data: [{     1     },{     2     },{     3    }        ] ,  1 }

Below we use for...in to traverse the JSON object

var arr = [];  for (var in datajson) {    if (attr! = ' Count ') {        Arr.push ( DATAJSON[ATTR])    }}

Well, it's convenient to turn JSON into the array we want, and then put the array back into JSON if necessary.

var json = {    Data:arr,    count:datajson[' count ']}console.log (JSON)

Well, look at the data that's handled well, a word--comfortable.

Special JSON object to the array, the most synthetic new JSON data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.