0

I have created a js object like this:

var JSONObject = {};

and a function a create an element an adds that to JSONObject :

function addShape(JSONObject, shapeStruct)
{
    var sh = "shape"+shapeStruct.id.toString();
    JSONObject.sh={
    "id": shapeStruct.id,
    "x1": shapeStruct.x1,
    "x2": shapeStruct.x2,
    "y1": shapeStruct.y1,
    "y2": shapeStruct.y2
    };
}

this function is in a separate file.

to call it i used:

shapeId++;
singleShapeStruct = {"id":shapeId, "x1":oldX, "x2":correctedX, "y1":oldY, "y2":yCanvas};
addShape(JSONObject, singleShapeStruct);
alert(JSONObject.shape0.x1);

the alert does not give any thing.

thanks for any kinf of help

  • FYI, there is nothing in your question or code that has anything to do with JSON. `{}` is an *object literal* here, which is a syntax construct to indicate an object. OTOH, JSON is a textual data-exchange format, like XML or CSV. See alse: [There is no such thing as a "JSON object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). – Felix Kling Jun 19 '13 at 08:51
  • @FelixKling i have been confusing really between JSON concept and JS objects, thanks, but can we say that JSON spec. requires JS object when it’s used in a string context?? –  Jun 19 '13 at 09:10
  • I'm not sure what you mean exactly, but I will add some more explanation: If you have a text file only containing, e.g. `["foo", "bar"]`, then you have JSON. If the file contains `var foo = ["foo", "bar"];`, then you have JavaScript creating an array. If you have `var foo = '["foo", "bar"]';` then you have JavaScript creating a string which contains data in JSON format. In JavaScript source code, JSON can only exist inside strings. You can parse such data to JS objects and arrays. – Felix Kling Jun 19 '13 at 09:16
  • it's quiet clear @FelixKling, thanks –  Jun 19 '13 at 09:23

3 Answers3

2

When you assign anything to JSONObject.sh, it won't use the variable sh. The name of the property will be "sh", not the value of the variable sh.

To use the variable to name the property, use the bracket syntax:

JSONObject[sh] = ...
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • How can I parse it and get the "x1" value for example? –  Jun 20 '13 at 07:32
  • @abualbara: There is no parsing needed, as it's already in the form of an object. You would parse it if you had a JSON string that you needed to access as an object. To access properties of an object inside your object, you can either use the property name as `JSONObject.shape0.x1` or the bracket syntax `JSONObject["shape0"].x1`. You can of course use a variable to specify the string in the bracket syntax. – Guffa Jun 20 '13 at 07:50
  • ooh thanks, I mean to get all object attributes: i used that: for(var i=0; i –  Jun 20 '13 at 07:58
1

Since you have a variable which has the actual key, you need to use the bracket notation instead of dot notation as the member operator

function addShape(JSONObject, shapeStruct)
{
    var sh = "shape"+shapeStruct.id.toString();
    JSONObject[sh]={
    "id": shapeStruct.id,
    "x1": shapeStruct.x1,
    "x2": shapeStruct.x2,
    "y1": shapeStruct.y1,
    "y2": shapeStruct.y2
    };
}
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

Here the JSONObject doesn't contains the sh property.

you have to initialize the JSONObject .sh object as

JSONObject[sh] = 
{
    "id" : .....
    "x1" : .....
    .
}

using Square Bracket notation for variable value as a name (dynamic) .. Not by dot Operator. dot operator requires static name of a property to be initialized.

Gokul E
  • 1,356
  • 2
  • 13
  • 28
  • *"dot operator of an object refers an existing property"* That is incorrect. `var foo = {}; foo.bar = 42;` works perfectly fine. – Felix Kling Jun 19 '13 at 08:52
  • yea u are right. @FelixKling.. but here the property inside the object is not static name. constructing as `sh = "shape"+shapeStruct.id.toString()`. `foo.bar` is the static name, but `sh` is not.. am i right ?? I am sorry if i was Wrong.... – Gokul E Jun 19 '13 at 08:57
  • Yes. If you want to use the value of a variable as property name, you have to use bracket notation. No worries, you can [edit] your answer and correct it :) – Felix Kling Jun 19 '13 at 09:00