简体   繁体   中英

JSON.parse values are individual characters?

why do i get each individual character coming out as a value like 0 : {

    memory=to;
    var store='[';


    $.each($('.spoke'),function(){
    store=store+'"'+$(this).attr('id')+'":"'+hexEncode('<div class="spoke"from="'+$(this).attr('from')+'"id="'+$(this).attr('id')+'">'+$(this).html()+'</div>')+'",';
    });


    store=store+']';store=store.replace(',]',']');





    $.localStorage.setItem(memory,JSON.stringify(store));

    var posts=$.localStorage.getItem(memory);

    posts=JSON.parse(posts);

    alert(posts);

    $.each(posts, function(key, value){

    alert(key+' : '+value);
    });

-------------------------wow im so damn tired its 3:15 its fixed

function save(){
    memory=to;
    var store={};


    $.each($('.spoke'),function(){
    var id=$(this).attr('id');

    var p=hexEncode('<div class="spoke"from="'+$(this).attr('from')+'"id="'+$(this).attr('id')+'">'+$(this).html()+'</div>');
    store[id]=p;
    });

    $.localStorage.setItem(memory,JSON.stringify(store));

    var posts=$.localStorage.getItem(memory);

    posts=JSON.parse(posts);


    $.each(posts, function(key, value){
    $('#log').append(hexDecode(value)+' it works');
    //$.each(value, function(key, value){
    //$('#log').append(value+' it works2');
    });//});

You've stringifyed a string so when you parse it you'll get back a string, and you'll just be reading the character at the specific index in the string. Instead of trying to build JSON by hand build an an array or object then stringify it.

You are not making an array but a string parseable to an array. You are also closing the quotes (") before you end the element. You either make a valid string or make a valid array ie.

store = [];
store.push("whatever");
$.localStorage.setItem(memory,JSON.stringify(store));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM