user2837459
user2837459

Reputation: 33

Encoding JavaScript object to JSON

I've got the problem. I tried to encode JavaScript Object to JSON. Here is a preview image of my Sencha app database object:

http://s11.postimg.org/ttelg6u0z/Przechwytywanie.png

I need to put it into local session, so I must encode this object. I tried using:

  1. JSON.stringify(db)
    and my result is only:
    {"version":"1.0"}

  2. Ext.Encode(db) - it's Sencha's function
    and in result I've got:
    undefinied

'db' is an object from the screen.

In need to encode this object with all of data.

Please, help me

[EDIT]

Is any other way to convert JS object to string and put it into local or session storage? Because object can't be stored in session or local storage

Upvotes: 1

Views: 695

Answers (2)

Moeri
Moeri

Reputation: 9294

Ext.Encode is intended to be used for route parameters, as you can see in this example JSON.stringify is intended to be used for constructing JSON strings from javascript objects.

Both do different things, what are you trying to achieve here?

As for why Ext.Encode returns undefined: is 'db' an object, or is it something else? Maybe it's because you have function properties in your object. I don't know how Ext.Encode is implemented, but they might only allow simple properties. That would explain why Ext.Encode doesn't work but JSON.stringify does. (JSON.stringify is a bit more lenient when it comes to function properties, etc.)

Upvotes: 0

Prog Mania
Prog Mania

Reputation: 624

JSON.stringify method doesn't convert functions or null/undefined variables, in your object you've only property "version" as a string value, other properties are functions and prototypes.

If undefined, a function, or an XML value is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array).

to know more about JSON.stringify, you can read this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

regarding the EXT, make sure that the db object identifies is declared in the ext scope.

Upvotes: 1

Related Questions