使用AJAX的jQuery解析JSON

使用AJAX的jQuery解析JSON

本文介绍了使用AJAX的jQuery解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用这个从jQuery的JSON解析:

  $。阿贾克斯({
    键入:GET,
    网址:HTTP://本地主机:8181 / salesmandata /',
    数据:{get_param:值},
    数据类型:JSON,
    成功:功能(数据){
        $每个(数据,功能(索引,元){
            执行console.log(元);
        });
    }
});
 

和这里的JSON响应:

所以,如果我会做

  $。每个(数据,功能(索引,元){
                执行console.log(元);
            });
 

它记录对象,成功和的 [数组[2],数组[2],数组[2],数组[2],数组[2],数组[2]] 的,其中包含的数据(swaldo,毛等)

我如何能做到这一点的反应我收到只有数组元素?我要存储在图表这些元素,我想这些在格式为:

  VAR数据= [
        {
            值:21006,
            名称:奥斯瓦尔多
        } 等等..
 

解决方案

使用的

你想要的阵列是在属性数据响应对象的数据

  VAR mappedData = data.data.map(函数(项目){
   返回{值:项目[1],名称:项目[0]};
});
 

I'm trying to parse json from jQuery using this:

$.ajax({
    type: 'GET',
    url: 'http://localhost:8181/salesmandata/',
    data: { get_param: 'value' },
    dataType: 'json',
    success: function (data) {
        $.each(data, function(index, element) {
            console.log(element);
        });
    }
});

and here's json response:

so if i'll do

$.each(data, function(index, element) {
                console.log(element);
            });

it logs object, success and [Array[2], Array[2], Array[2], Array[2], Array[2], Array[2]] which contains data (swaldo, mao etc.)

How can I do that on response I recieve only that array elements? I want to store these elements in chart and I want these in that format:

var data=[
        {
            value: 21006,
            name: "Oswaldo"
        } etc..
解决方案

Use Array.prototype.map()

The array you want is in the property data of the response object data

var mappedData = data.data.map(function(item){
   return { value: item[1], name: item[0] };
});

这篇关于使用AJAX的jQuery解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 03:42