0

I have this code to save values ​​in json string in a session variable, which I call from ajax

I have the following code:

(function ($) { 

    Drupal.behaviors.MyfunctionTheme = {
        attach: function(context, settings) {

     $('.add-music').click(function () {
         var songNew = JSON.stringify({
             title: $(this).attr('data-title'),
             artist: $(this).attr('data-artist'),
             mp3: $(this).attr('href')
         });
         var songIE = {json:songNew};
         $.ajax({
             type: 'POST',
             data: songIE,
             datatype: 'json',
             async: true,
             cache: false                
         })
         .done(
              //this is the callback function, which will run when your POST request returns
            function(postData){
                //Make sure to test validity of the postData here before issuing the GET request
                var session;
                $.ajaxSetup({cache: false})
                $.get('/getsession.php', function (getData) {
                      session = getData;
                        alert(session);
                });

              }
         );

     });

}}

})( jQuery );

I have the following code that works fine, I just printed the following alert:

["{\"title\":\"El Derecho de las Mujeres a La Comunicaci\u00f3n\",\"artist\":\"\",\"mp3\":\"http:\/\/red.comppa.com\/sites\/default\/files\/audios\/Cun%CC%83aDerechoMujeresaLaComunicacion.mp3\"}","{\"title\":\"Objetivos del encuentro internacional de Derechos Humanos en el Bajo Aguan.\",\"artist\":\"\",\"mp3\":\"http:\/\/red.comppa.com\/sites\/default\/files\/audios\/objetivos_del_encuentro_dh.mp3\"}"]

and I need to have something like this:

[
  {
    title:"Cro Magnon Man",
    artist:"The Stark Palace",
    mp3:"http://www.jplayer.org/audio/mp3/TSP-01-Cro_magnon_man.mp3"
  },
  {
    title:"Hidden",
    artist:"Miaow",
    mp3:"http://www.jplayer.org/audio/mp3/Miaow-02-Hidden.mp3"
  }
]

How I can work this data with jquery?

thank's

4

4 に答える 4

2

データが変数に格納されていると仮定すると、次のyourObjectことができます。

var result = JSON.parse("["+yourObject[0]+"]");

こちらがワーキングビン

于 2013-03-28T19:31:04.427 に答える
0

サンプル入力は、実際には文字列の配列です。配列内の各要素をオブジェクトとして解析する必要があります。jQuery の使用:

var input = ["{\"title\":\"El Derecho de las Mujeres a La Comunicaci\u00f3n\",\"artist\":\"\",\"mp3\":\"http:\/\/red.comppa.com\/sites\/default\/files\/audios\/Cun%CC%83aDerechoMujeresaLaComunicacion.mp3\"}","{\"title\":\"Objetivos del encuentro internacional de Derechos Humanos en el Bajo Aguan.\",\"artist\":\"\",\"mp3\":\"http:\/\/red.comppa.com\/sites\/default\/files\/audios\/objetivos_del_encuentro_dh.mp3\"}"];

var resultArray = new Array();

for(var i = 0; i < input.length; i++){
    var parsedElement = $.parseJSON(input[i]);
    resultArray.push(parsedElement);
}
于 2013-03-28T19:36:32.653 に答える
0

これJSON.parse()が目的で設計されたものです。Eval()ここで使用するアプローチではありません。明確にするためにJSON.parse()、適切にエスケープされた有効な JSON 文字列を取得し、Javascript 内で使用可能なオブジェクトに変換します。eval()一方、 は、文字列を取り、それらを Javascript 関数、オブジェクト、変数などとして実行しようとするように設計されています。

于 2013-03-28T19:36:05.667 に答える
0
var display = JSON.stringify(jsonObject, undefined, 2); // indentation level = 2
于 2013-03-28T19:38:09.020 に答える