0

我有下面的json数组

`[{"name":"The Shawshank Redemption","rating":"9.3","year":"1994","stars":["Tim Robbins","Morgan Freeman","Bob Gunton"],},{"name":"The Godfather","rating":"9.2","year":"1972","stars":["Marlon Brando","Al Pacino","James Caan"]}]`

我想在 javascript 数组中转换并打印为 html。然后我会复制数组并保存在 .js 文件中,如下所示。问题是如何从“名称”中删除倒昏迷到名称

var Movies =  [ { name: 'The Shawshank Redemption',
rating: '9.3',
year: '1994',
stars:
 [ 'Tim Robbins',
   'Morgan Freeman',
   'Bob Gunton' ]},
{ name: 'The Godfather',
rating: '9.2',
year: '1972',
stars:
 [ 'Marlon Brando',
   'Al Pacino',
   'James Caan' ]}
];
4

4 回答 4

1
var movies= $.parseJSON(myStr); put your string in it

用于印刷

var newAry=[];
$.each(movies,function(i,v){
  newAry.push(JSON.stringify(v));
});

$( '#div' ).html(newAry);
于 2013-09-10T09:17:01.237 回答
1

如果你想漂亮地打印这个数组,例如到一个<pre>容器中,你可以使用JSON.stringify()这两个参数:

// assuming your parsed JSON is in this variable
var yourObj = JSON.parse( "[your JSON in here]" );

// pretty print
var pretty = JSON.stringify( yourObj, null, '  ' );
document.getElementById( 'target' ).innerHTML = pretty;
于 2013-09-10T09:21:09.727 回答
0

你的意思是 :

var a = [];
a.push($.parseJSON('{"a":"b"}'));

?

于 2013-09-10T09:20:20.487 回答
0

好吧,我认为上面的 json 数组不是正确的 JSON 结构。首先,我们需要从您的 json 数组的第 120 个索引中删除逗号 (,)。现在它会像这样..

 var jsonArray = `[{"name":"The Shawshank Redemption","rating":"9.3","year":"1994","stars":["Tim Robbins","Morgan Freeman","Bob Gunton"]},{"name":"The Godfather","rating":"9.2","year":"1972","stars":["Marlon Brando","Al Pacino","James Caan"]}]` ;

现在使用 json.parse()

IE

var Movies = JSON && JSON.parse(jsonArray);

希望对你有帮助..

于 2013-09-10T10:03:12.470 回答