English 中文(简体)
如何使用 jquery 分析json 对象
原标题:How to parse a json object using jquery
  • 时间:2012-05-26 19:31:06
  •  标签:
  • jquery
  • json

我认为这是一个非常基本的问题,但我花了数小时来寻找答案,却毫无运气,我有以下json 对象(通过游戏产生! Framework)

{
    "preg.pregunta": [{
        "message":"Debes escribir una pregunta",
        "key":"preg.pregunta",
        "variables":[]
    }],
    "preg":[{
        "message": "validation.object",
        "key":"preg",
        "variables":[]
    }]
}

这是我的密码

$.ajax({
    type: $target.attr( method ),
    data: dataString,
    url:$target.attr( action ),
    dataType: "json",
    beforeSend: function() {
        //some stuff
    },
    success:function(response){
        //some stuff
    },
    error: function(response){
        //I want to use the json response here.
    }
});

我要全部进入 pregunta ( messessage keey 值)

有帮助吗?

“强势”UPDed

Ok Ok 说不定会更明显, 或者我还要多研究一点; 我发现jQuery没有正确分析JSON的反应,

有人知道为什么会这样吗?

我刚在 success handler 中测试了这个代码, 并且运行良好!

$.ajax({
  type: $target.attr( method ),
  data: dataString,
  url:$target.attr( action ),
  dataType: "json",
  beforeSend: function() {
  },
  success:function(response){
    //It is working perfectly!
    $.each(response,function(object){ //first loop of the object
      $.each(response[object],function(values){ //looping inside arrays
        console.log(response[object][values].key) //getting value "key"
        console.log(response[object][values].message) //getting value "message"
      });
    })
  },
  error: function(response){
    //nothing happens here
  }
});

更新二号

在搜索了大约两个小时后, 我找到了一个直接的解决办法:

error: function(response){
//Note the jQuery.parseJSON function
var response = jQuery.parseJSON(response.responseText);
  $.each(response,function(object){
    $.each(response[object],function(values){
      console.log(response[object][values].key)
      console.log(response[object][values].message)
    });
  })
}

解释 : 当使用错误处理器时, jQuery 返回一个描述错误的复杂对象, 响应Text 包含从服务器上检索的数据, 所以, 您必须使用 剖析JSON 函数来分析它 。

希望这有帮助!

最佳回答

尝试此 :

error: function(response) {
    var pregunta = response["preg.pregunta"][0].message;
    var key = response["preg.pregunta"][0].key;
},

如果 pregunta 阵列中存在多个值,您需要循环,用迭代变量替换 [0]

而且,我不明白为什么你只想要访问 error 处理器中的应答词?

最后,访问 JSON 的代码是本地的 Javascript 。 这是因为当您收到回复时, 它已被转换成 POJS 对象, 不需要jQuery 。

问题回答

暂无回答




相关问题
JQuery/MVC Search Issue

I have inherited a piece of work where the entry screen shows a summary of 20 calculated variables. E.g. Var A (250), Var B (79). Clicking on any of these links takes the user to a view with a ...

jQuery quicksearch plug-in tinkering with JSON

I ve implemented the quicksearch plugin by Rik Lomas and I love it for an application in a custom CMS I m building. I was wondering though, since I m going to have a bizillion items in the table if ...

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

PHP json_decode question

i m trying to use json_decode to combine a few json objects and then re-encode it. my json looks like: { "core": { "segment": [ { "id": 7, "...

Converting JSON data to Java object

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string ...

热门标签