www.webdeveloper.com
Results 1 to 10 of 10

Thread: JSON.parse/JSON.stringify Array - Remove special characters

  1. #1
    Join Date
    Mar 2013
    Posts
    13

    JSON.parse/JSON.stringify Array - Remove special characters

    I am using the following code:

    HTML Code:
        // History Information Array
        var historyInfo = localStorage.getItem("history");
        if (!historyInfo) {
            historyInfo = [];
        } else {
            historyInfo = JSON.parse(historyInfo);
        }
        historyInfo.push("You travelled " + distance + " mile(s) in " + duration + " on " + timestamp + "." + "<br>");
    	
        localStorage.setItem("history", JSON.stringify(historyInfo));
    This works fine!

    However, when I return the results to the web page using
    HTML Code:
    localStorage.getItem("history")
    it returns something like this:

    ["You travelled 0 in mile(s) 00:00.05 on 11 April 2013 18:02:33.
    ","You travelled 0 mile(s) in 00:00.08 on 11 April 2013 18:03:31.
    "]

    How can I remove the square brackets and quotation marks?

  2. #2
    Join Date
    Dec 2012
    Posts
    100
    The square brackets indicate that it's an array, so your example there is an array containing 2 strings.

    If you want to output both of the strings you could do something like this

    HTML Code:
    for(var i = 0; i < historyInfo.length; i++) {
        document.body.innerHTML += historyInfo[i];
    }

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,332
    This is not PHP. I'll move it to the JavaScript forum, since that's what it appears to me to be.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Mar 2013
    Posts
    13
    iBeZi, the code you provided should work. However, I am using the following code:

    function clearWatch(position) {
    document.getElementById('start').disabled = false;
    document.getElementById('stop').disabled = true;
    navigator.geolocation.clearWatch(position);
    endTime = new Date();

    // Duration Calculator
    var difference = endTime - startTime;
    var diffSeconds = difference / 1000;
    var HH = Math.floor(diffSeconds / 3600);
    var MM = (Math.floor(diffSeconds % 3600) / 60).toFixed();
    var duration = ((HH < 10) ? ("0" + HH) : HH) + ":" + ((MM < 10) ? ("0" + MM) : MM);

    // History Information Array
    var historyInfo = localStorage.getItem("history");
    if (!historyInfo) {
    historyInfo = [];
    } else {
    historyInfo = JSON.parse(historyInfo);
    }
    historyInfo.push("You travelled " + distance + " mile(s) in " + duration + " on " + timestamp + "." + "<br>");

    localStorage.setItem("history", JSON.stringify(historyInfo));
    }

    // localStorage
    if (typeof (Storage) !== "undefined") {
    if (localStorage.getItem("history") === null) {
    document.getElementById("history").innerHTML = "There are currently no records to display.";
    } else {
    document.getElementById("history").innerHTML = localStorage.getItem("history");
    }
    } else {
    document.getElementById("history").innerHTML = "Your browser does not support the localStorage API.";
    }

    As you can see, I'm using 2 functions. When I apply the code you provided it doesn't work. Any ideas?

  5. #5
    Join Date
    Dec 2012
    Posts
    100
    In this part

    Code:
    if (localStorage.getItem("history") === null) {
    document.getElementById("history").innerHTML = "There are currently no records to display.";
    } else {
    document.getElementById("history").innerHTML = localStorage.getItem("history");
    }
    You need to parse the JSON stored in the local storage before outputting each item from the array to the history element

    Code:
    if (localStorage.getItem("history") === null) {
    document.getElementById("history").innerHTML = "There are currently no records to display.";
    } else {
    var historyArray = JSON.parse(localStorage.getItem("history"));
    for(var i = 0; i < historyArray.length; i++) {
    document.getElementById("history").innerHTML += historyArray[i]+" ";
    }
    }

  6. #6
    Join Date
    Mar 2013
    Posts
    13
    iBeZi, don't worry! I resolved this problem by moving the History Information Array outside of the clearWatch function. Success!

    I will ask one more question. Is there anyway to append a localStorage.removeItem(key) button to each record? And if so, how could I incorporate it within my existing code? Thanks

  7. #7
    Join Date
    Dec 2012
    Posts
    100
    You could do it by making a button for each record, making a note of which index that record appears at in the history array (maybe as a data attribute on the button), then when the button is clicked splice that record from the history array then save and update the record list.

  8. #8
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by iBeZi View Post
    You could do it by making a button for each record, making a note of which index that record appears at in the history array (maybe as a data attribute on the button), then when the button is clicked splice that record from the history array then save and update the record list.
    that sounds dangerous; you forgot to instruct that the indices need rebuilt+ re-rendered every deletion (not just saved), or else the splice will make further delete buttons delete the wrong ones.

  9. #9
    Join Date
    Dec 2012
    Posts
    100
    Well when I said save and update the records list I meant outputting the list with buttons again with the new array, something like this http://jsfiddle.net/pQ5R2/

  10. #10
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    cool. i kinda mis-read your post and just wanted to be safe... cheers!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles