如何使用jquery/javascript处理Json

如何使用jquery/javascript处理Json,jquery,ajax,json,grails,Jquery,Ajax,Json,Grails,嗨,我的setDefaultPoint控制器以json格式返回映射。。。代码如下 def setDefaultPoint = { if (springSecurityService.isLoggedIn()) { def officeId = params.officeId def officeRoleInstance=OfficeRole.get(officeId) def company= officeRoleIns

嗨,我的setDefaultPoint控制器以json格式返回映射。。。代码如下

def setDefaultPoint = {
    if (springSecurityService.isLoggedIn()) {
        def officeId = params.officeId          
        def officeRoleInstance=OfficeRole.get(officeId)
        def company= officeRoleInstance.company
        def defaultPoint = officeRoleInstance.distanceChart.officePoint
        def map = [defaultPoint:defaultPoint]
        map << [company:company]
        def json = map as JSON
        render json
    }
我从json传递两个不同的对象。。那么如何得到这些物体的好处。。。defaultPoint对象和Company对象都有名为id和anme的字段

答案是

 {"defaultPoint":{"class":"com.springpeople.steer.trips.Point","id":3,"name":"MG road"},"company":{"class":"com.springpeople.steer.partymodel.parties.Company","id":5,"addressPermanent":{"class":"Address","id":3},"contactDetails":null,"name":"Infosys","offices":[{"class":"OfficeRole","id":6}],"organizationRoles":[{"class":"OrganizationRole","id":5}],"panNumber":null,"serviceTaxNumber":null,"tanNumber":null}}
然后您可以访问类似于
obj.class
之类的内容

文档:

然后您可以访问类似于
obj.class
之类的内容


文档:

如果您设置了
数据类型:“json”
您不需要在成功回调中解析json

alert(data.class);

如果您已经设置了
数据类型:“json”
您不需要在成功回调中简单地
解析json

alert(data.class);

由于返回的数据类型被指定为
json
,因此传递给分配给
success
的函数的
data
参数将是从返回的json字符串解析的JavaScript对象,只要该字符串是有效的json。ajax调用中传递的
数据
不需要周围的参数。这应该行得通

function setDefaultPoint(){
    var officeId =  $('#clientTrip_officeId').val();

    $.ajax({
        url:"${createLink(controller:'clientTrip',action:'setDefaultPoint')}",
        dataType: "json",
        data: { officeId: officeId },
        success: function(data) {
            console.log(data.defaultPoint.id, data.defaultPoint.name);
            console.log(data.company.id, data.company.name);
        }

    });
}

由于返回的数据类型被指定为
json
,因此传递给分配给
success
的函数的
data
参数将是从返回的json字符串解析的JavaScript对象,只要该字符串是有效的json。ajax调用中传递的
数据
不需要周围的参数。这应该行得通

function setDefaultPoint(){
    var officeId =  $('#clientTrip_officeId').val();

    $.ajax({
        url:"${createLink(controller:'clientTrip',action:'setDefaultPoint')}",
        dataType: "json",
        data: { officeId: officeId },
        success: function(data) {
            console.log(data.defaultPoint.id, data.defaultPoint.name);
            console.log(data.company.id, data.company.name);
        }

    });
}

由于您已将数据类型指定为Json,jQuery将把响应解析为一个对象,因此您应该能够使用:

function setDefaultPoint(){
        var officeId =  $('#clientTrip_officeId').val();

    $.ajax({
        url:"${createLink(controller:'clientTrip',action:'setDefaultPoint')}",
        dataType: "json",
        data:({officeId: officeId}),
        success: function(data) {

            // here i want to get the id and name of default point and id and name of company...
    console.log(data.defaultPoint.Id)         
        }

    }); 
}

由于您已将数据类型指定为Json,jQuery将把响应解析为一个对象,因此您应该能够使用:

function setDefaultPoint(){
        var officeId =  $('#clientTrip_officeId').val();

    $.ajax({
        url:"${createLink(controller:'clientTrip',action:'setDefaultPoint')}",
        dataType: "json",
        data:({officeId: officeId}),
        success: function(data) {

            // here i want to get the id and name of default point and id and name of company...
    console.log(data.defaultPoint.Id)         
        }

    }); 
}

默认的point对象和company对象都有一个名为id和name的字段,用于获取those@Russ很好的一点是,您不需要解析JSON,因为数据类型设置为JSON。要访问这些字段,请执行以下操作:
data.defaultPoint.id
data.company.id
默认点对象和company对象都有一个名为id和name的字段如何获取those@Russ很好的一点是,您不需要解析JSON,因为数据类型设置为JSON。要访问这些字段,请执行以下操作:
data.defaultPoint.id
data.company.id