1

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

3
  • Looks like db is not an object literal but a function / class instance Commented Oct 2, 2013 at 6:47
  • i don't think u can encode functions into json Commented Oct 2, 2013 at 6:50
  • 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 Commented Oct 2, 2013 at 7:17

2 Answers 2

1

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.

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

10 Comments

Ok, thanks. This explain why it doesn't work. Is any method to convert JavaScript objects with properties, functions and prototypes into JSON?
@user2837459 the question here is why you want to convert a method to a string? if it's a library you can call it later, you'd better use the local storage for storing data only, note: localstorage is limited storage, and parsing objects consumes processing.
Because I need to store object instance returned by openDatabase() method and use this after reload my application.
@user2837459 well, but you can reopen the connection when ever you need, why storing the connection variable?
In my app I'm opening and closing another webView and after that database objects returned from openDatabase() are different. So I've stored first instance of my database in global variable, but when I have to reload app this object dissapeared. That's why I want to store this connection variable.
|
0

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.)

1 Comment

Yes, it's an object. I don't know why it's undefined. In Ext.Encode is posible to convert JS objects to JSON. Here's example: docs.sencha.com/core/manual/content/utility.html#util-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.