0

We're serializing a form using serializeObject and then storing this into local storage as a JSON encoded string. When we decode this back out of LocalStorage, we have keys with the name of flight[inbound][0][iso] and flight[outbound][1][date] etc. This makes it difficult to properly loop through later on (in JavaScript) as we need to now repopulate the form with the stored data.

Is there a way of further decoding these key names into sub-objects? For example:

flight[outbound][0][datetime] = "Something";
flight[outbound][0][from]     = "Something";
flight[outbound][0][to]       = "Something";
flight[outbound][0][carrier]  = "Something";
flight[inbound][0][datetime]  = "Something";
flight[inbound][0][from]      = "Something";
flight[inbound][0][to]        = "Something";
flight[inbound][0][carrier]   = "Something";

Should turn into:

flight = {
    outbound: {
        0: {
            datetime: "Something",
            from:     "Something",
            to:       "Something",
            carrier:  "Something",
        }
    },
    inbound: {
        0: {
            datetime: "Something else",
            from:     "Something else",
            to:       "Something else",
            carrier:  "Something else",
        }
    }
}
5
  • What's the code of serializeObject ? This should be wrong on the step of serializing to the right object. Commented Apr 24, 2014 at 9:54
  • I lied. It's gist.github.com/jbrooksuk/11248775 Commented Apr 24, 2014 at 9:54
  • What does the JSON look like? If your turning JSON into a JS object you should use JSON.Parse() Commented Apr 24, 2014 at 9:55
  • @James So it's a jQuery plugin? Any way, could you create a jsfiddle to reproduce your problem? Commented Apr 24, 2014 at 9:56
  • Also see Javascript expando objects flight[outbound] is directly equal to flight.outbound or flight={outbound:{...}} Commented Apr 24, 2014 at 10:00

1 Answer 1

0

It turns out that the serializeObject plugin we're using wasn't doing the right job. Switching it for https://github.com/macek/jquery-serialize-object has provided us with what we're after.

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

1 Comment

You know this is build into JavaScript now don't you? You don't need a plugin at all. Also the default polyfill Json serializer is http://bestiejs.github.io/json3/. This is the one built on JSON2 which was written by the guy who invented JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.