循环JSON.stringify并在SELECT上显示它

时间:2015-03-25 12:32:45

标签: jquery json

var new_result = JSON.stringify(result);
// this is the info of new_result
{
    "1":"Motor-boats : Motor-yachts > 50'",
    "2":"Motor-boats : Super-yachts > 100'",
    "3":"Motor-boats : Mega-yachts > 150'"
}

//loop if how many key value pair there is
$('#vessel_sub_category_id').append($('<option>', { value: 1, text: "Motor-boats : Motor-yachts > 50'" }));

实际上先生我找到了这个代码..当我把它隔离起来它有效但是当我把它放在ajax帖子上成功它不起作用我不知道为什么..

此代码位于ajax帖子中。

  var obj = JSON.stringify(result);
  $.each( obj, function( key, value ) {
  alert( key + ": " + value );
  });

1 个答案:

答案 0 :(得分:0)

你试图以相反的方式解决它,你需要解析JSON而不是stringify,然后将它用作JS对象:

var obj = JSON.parse(result);
$.each(obj, function(key, value) {
    alert(key + ": " + value);
});