使用ajax jQuery解析JSON

时间:2015-10-27 19:49:23

标签: javascript jquery arrays json ajax

我正在尝试使用以下方法从jQuery解析json:

< property>
<name>hive.metastore.uris</name>
<value>thrift://server1.example.com:9083</value>
</property>
<property>
<name>hive.metastore.sasl.enabled</name>
<value>true</value>
<description>If true, the metastore thrift interface will be secured with SASL. Clients must authenticate with Kerberos.</description>
< /property>
<property>
<name>hive.metastore.kerberos.keytab.file</name>
<value>/etc/hive/conf/hive.keytab</value>
<description>The path to the Kerberos Keytab file containing the metastore thrift server’s service principal.</description>
< /property>
<property>
<name>hive.metastore.kerberos.principal</name>
<value>hive/_HOST@example.COM</value>
<description>The service principal for the metastore thrift server. The special string _HOST will be replaced automatically with the correct host name.</description>
< /property>

这里是json回复:

  

{ “resultDescription”: “SUCCESS”, “数据”:[[ “的Oswaldo”, “21006”],[ “毛”, “52311”],[ “安吉莉娜”, “45300”],[“赫拉尔”, “36467”],[ “尼基”, “17099”],[ “测试”, “28682”]]}

所以,如果我要做的话

$.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);
        });
    }
});

它记录了包含数据的对象,成功和 [Array [2],Array [2],Array [2],Array [2],Array [2],Array [2]] (swaldo,mao等)

我怎么能在响应时才这样做我只收到数组元素?我想将这些元素存储在图表中,我想要这种格式:

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

1 个答案:

答案 0 :(得分:4)

使用Array.prototype.map()

您想要的数组位于响应对象Scanner scanner = new Scanner(System.in); boolean inputMismatch; int number; do { try { System.out.println("Enter a number"); number = Integer.parseInt(scanner.nextLine()); inputMismatch = false; } catch (NumberFormatException e) { e.printStackTrace(); inputMismatch = true; } } while (inputMismatch);

的属性data
data