jQuery json解析 [英] jquery json parsing

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

问题描述

如何使用jQuery解析此json?

How do I parse this json with jQuery?

DayEvents : [{"0":"886","event_id":"886","1":"5029","user_id":"5029","2":"Professional","user_type":"Professional", ...

推荐答案

术语解析"有点放错了位置,因为它已经是JSON格式.您无需解析它,而只需对其进行访问.如果它是JSON格式的大String,则确实需要在访问之前先将其解析为可用的JSON对象.

此JSON包含一个属性DayEvents,该属性又包含一个数组[].您可以使用点.运算符访问属性.您可以使用[index]在给定索引处获得数组项,其中零0表示第一项.

This JSON contains one property, the DayEvents, which in turn contains an array []. You can access properties using dot . operator. You can get an array item at the given index using [index] where zero 0 denotes the first item.

var json = { DayEvents : [{"0":"886","event_id":"886","1":"5029","user_id":"5029","2":"Professional","user_type":"Professional" }]};
var firstDayEvent = json.DayEvents[0];

该数组又包含一个对象{}.也许不止一个?数组中可以有多个项目,然后应该看到[{}, {}, {}, ...],然后可以像这样循环访问每个项目:

The array in turn contains an object {}. Or maybe more than one? You can have more than one items in an array, you should then see [{}, {}, {}, ...] and you could then access each item in an loop like so:

for (var i = 0; i < json.DayEvents.length; i++) {
    var dayEvent = json.DayEvents[i];
    // ...
}

单日事件对象具有多个属性:0event_id1user_id2等.您不能使用点.运算符访问以数字开头的属性,您然后想使用大括号符号:

A single day event object has several properties: 0, event_id, 1, user_id, 2, etc. You cannot access properties starting with a number using dot . operator, you would then like to use the brace notation:

var zero = firstDayEvent['0'];
var eventId = firstDayEvent.event_id;
var one = firstDayEvent['1'];
var userId = firstDayEvent.user_id;
var two = firstDayEvent['2'];
// ...

alert(eventId); // 886
alert(two); // Professional

要了解有关JSON的更多信息,请查看本教程.

To learn more about JSON, check this tutorial.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆