2

I'm having a difficult time with something that should be (and probably is) pretty basic. I have a constructor as follows:

function search_table(v1, v2, v3) {
    this.field = v1;
    this.condition = v2;
    this.value = v3;
}

My application has an array of search_table which gets JSON.stringify(ied) and then stored in localStorage.

My question is... when I retrieve the object from localStorage, it's in JSON string format. How do I get it back into the search_table[] format?

I'd prefer to do this without jQuery if possible. Thanks

I've tried to use something like the following:

var search_array = JSON.parse(string_val);

But it doesn't allow me to access search_array[i].condition as an example.

4
  • 1
    This should work. Please provide an example of what you're getting back in string_val. Commented Jan 21, 2012 at 20:10
  • When I do: alert(search_array) it looks the same as the JSON (I assume this is normal). In the JavaScript console it says 'Uncaught TypeError: Cannot read property 'condition' of undefined.' Commented Jan 21, 2012 at 20:14
  • 1
    Note: Some browsers do not support JSON.stringify, perhaps this could be affecting your application. Commented Jan 21, 2012 at 20:16
  • Travis - JSON.stringify and JSON.parse both work as expected (outputs are below). Commented Jan 21, 2012 at 21:30

1 Answer 1

2

This should work. The following code:

function search_table(v1, v2, v3) {
    this.field = v1;
    this.condition = v2;
    this.value = v3;
}
var arr = [new search_table(1, 2, 3), new search_table(4, 5, 6), new search_table(7, 8, 9)];

var str = JSON.stringify(arr);
console.log("stingified: ", str);

var search_array = JSON.parse(str);
var result = search_array[1].condition;
console.log("result: ", result);

Gives the following output:

stingified: [{"field":1,"condition":2,"value":3},{"field":4,"condition":5,"value":6},{"field":7,"condition":8,"value":9}]
result: 5

Your issue is elsewhere in the code that you haven't included here.

I'd also check Travis's suggestion - does your browser have native support for the JSON functions? Try these, and make sure they don't return "undefined":

alert(JSON.stringify);
alert(JSON.parse);

Also, make sure you initialized i, and that it is in-bounds of your array.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Ziesemer. I noticed when I alert(search_array) from my code above it shows the JSON string, but when I alert(search_array) from your example, it shows [object Object],[object Object],[object Object]. alert(JSON.stringify) returns function stringify() { [native code] } as expected, as does alert(JSON.parse). Any ideas?
I just tried alert(search_array[0]) from mine and it is treating search_array like a string. i.e. search_array[0] = '[', 1 is '{', 2 is '"', 3 is 'f', and so on.
@haxor - Please output and provide an actual example of what you're seeing in your read string. Clearly, something isn't right here. Showing all of the [object Object] just means that your browser doesn't have the best toString implementation on Array, but everything is still as expected.
I figured it out, and it's strange. I had to JSON.parse(JSON.parse(...)) my variable for some reason. When I call into my local_storage_persist_object (which is a localStorage.setItem wrapper), I'm performing a JSON.stringify on its val. I'm wondering if localStorage.getItem doesn't automatically perform a JSON.parse. Any idea? FWIW the first time I perform a JSON.parse and the second time I perform a JSON.parse, the values are the same, but in the first case the quotes are escaped, whereas they are not in the second.
@haxor - I think I'd need to see your complete code to understand - but I'm glad you figured it out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.