-8

Working SO code below:

var property_object_parse = jQuery.parseJSON('{"p1":{"TextElement":[{"yscale":"100","xscale":"100","shadow":"false","rotation":"","id":"#1_company name","text":"Company name","font":"Plantagenet Cherokee","size":"9","h":"0.16","w":"1.15","y":"0.3","x":"0.53","lock":"false","customize":"false","Alpha":"100","underline":"false","italic":"","blockName":"company name","caption":"Company name","textType":"Company name","bold":"false","colorspace":"DeviceRGB","colorcode":"rgb(1.00,1.00,1.00)","color":"16777215","hex_color":"","valign":"center","align":"left","sortorder":"0","inputformat":false,"format":false,"clippath":false,"inputrequired":false},{"yscale":"100","xscale":"100","shadow":"false","rotation":"","id":"#1_first name","text":"First name","font":"Plantagenet Cherokee","size":"7","h":"0.1","w":"1.14","y":"0.44","x":"2.3","lock":"false","customize":"false","Alpha":"100","underline":"false","italic":"","blockName":"first name","caption":"First name","textType":"First name","bold":"false","colorspace":"DeviceRGB","colorcode":"rgb(1.00,1.00,1.00)","color":"16777215","hex_color":"","valign":"center","align":"right","sortorder":"1","inputformat":false,"format":false,"clippath":false,"inputrequired":false},{"yscale":"100","xscale":"100","shadow":"false","rotation":"","id":"#1_last name","text":"Last Name","font":"Plantagenet Cherokee","size":"6","h":"0.11","w":"1.16","y":"0.56","x":"2.3","lock":"false","customize":"false","Alpha":"100","underline":"false","italic":"","blockName":"last name","caption":"Last Name","textType":"Last Name","bold":"false","colorspace":"DeviceRGB","colorcode":"rgb(1.00,1.00,1.00)","color":"16777215","hex_color":"","valign":"center","align":"right","sortorder":"2","inputformat":false,"format":false,"clippath":false,"inputrequired":false},{"yscale":"100","xscale":"100","shadow":"false","rotation":"","id":"#1_email","text":"Email","font":"Plantagenet Cherokee","size":"6","h":"0.1","w":"1.14","y":"0.69","x":"2.3","lock":"false","customize":"false","Alpha":"100","underline":"false","italic":"","blockName":"email","caption":"Email","textType":"Email","bold":"false","colorspace":"DeviceRGB","colorcode":"rgb(1.00,1.00,1.00)","color":"16777215","hex_color":"","valign":"center","align":"right","sortorder":"3","inputformat":false,"format":false,"clippath":false,"inputrequired":false},{"yscale":"100","xscale":"100","shadow":"false","rotation":"","id":"#1_phone","text":"Phone","font":"Plantagenet Cherokee","size":"6","h":"0.11","w":"1.14","y":"0.81","x":"2.3","lock":"false","customize":"false","Alpha":"100","underline":"false","italic":"","blockName":"phone","caption":"Phone","textType":"Phone","bold":"false","colorspace":"DeviceRGB","colorcode":"rgb(1.00,1.00,1.00)","color":"16777215","hex_color":"","valign":"center","align":"right","sortorder":"4","inputformat":false,"format":false,"clippath":false,"inputrequired":false},{"yscale":"100","xscale":"100","shadow":"false","rotation":"","id":"#1_street","text":"street","font":"Plantagenet Cherokee","size":"9","h":"0.17","w":"1.16","y":"0.95","x":"2.3","lock":"false","customize":"false","Alpha":"100","underline":"false","italic":"","blockName":"street","caption":"street","textType":"street","bold":"false","colorspace":"DeviceRGB","colorcode":"rgb(1.00,1.00,1.00)","color":"16777215","hex_color":"","valign":"center","align":"right","sortorder":"5","inputformat":false,"format":false,"clippath":false,"inputrequired":false}]},"p2":{"ImageElement":[{"type":"image","text":"Sample.png","blockName":"Back Logo","id":"#2_Back Logo","lock":"false","rotation":"0","h":"0.68","w":"0.69","y":"0.59","x":"0.41","valign":"bottom","halign":"left","clippath":false}],"TextElement":[{"yscale":"100","xscale":"100","shadow":"false","rotation":"","id":"#2_Company Name","text":"Company Name","font":"Plantagenet Cherokee","size":"9","h":"0.16","w":"1.16","y":"1.3","x":"0.24","lock":"false","customize":"false","Alpha":"100","underline":"false","italic":"","blockName":"Company Name","caption":"Company Name","textType":"Company Name","bold":"false","colorspace":"DevicaeRGB","colorcode":"rgb(1.00,0.00,0.00)","color":"16711680","hex_color":"","valign":"bottom","align":"center","sortorder":"0","inputformat":false,"format":false,"clippath":false,"inputrequired":false},{"yscale":"100","xscale":"100","shadow":"false","rotation":"","id":"#2_Tagline","text":"Sample Tagline","font":"Helvetica","size":"12","h":"0.18","w":"0.73","y":"1.52","x":"0.25","lock":"false","customize":"false","Alpha":"100","underline":"false","italic":"","blockName":"Tagline","caption":"Sample Tagline","textType":"Sample Tagline","bold":"false","colorspace":"DeviceRGB","colorcode":"rgb(1.00,1.00,1.00)","color":"16777215","hex_color":"","valign":"top","align":"left","sortorder":"1","inputformat":false,"format":false,"clippath":false,"inputrequired":false}]}}');
var selected_element = 'p1.TextElement[0].size';
console.log(property_object_parse.selected_element);

I am getting undefined when I try to get the value using variable. Is this correct way to access it?

See my jsfiddle here

poke
  • 369,085
  • 72
  • 557
  • 602
Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
  • Why did you not posted the code here at SO. See: [How to create a Minimal, Complete, and Verifiable example.](http://stackoverflow.com/help/mcve) – Satpal Dec 14 '15 at 09:20
  • 4
    Hello, won't say _Welcome to SO!_ as you've been here for a long time now. And you should know that when asking question, the code should be added here on SO. I strongly insist to see [ask] and [mcve]. – Tushar Dec 14 '15 at 09:21
  • no one have answer of question but all are ready for minus voting..is this stackoverflow level? – Bhumi Shah Dec 14 '15 at 09:21
  • Apparently disregarding rules when you very well know what they are and willfully ignoring the instructions when you tried to post the jsfiddle link is the Stack Overflow level. – JJJ Dec 14 '15 at 09:22
  • 1
    @Tushar When user add code here then you guys will tell them create jsfiddle and show and if user create fiddle, you will tell them to write code. – Bhumi Shah Dec 14 '15 at 09:22
  • @Satpal: I have added fiddle, you can check that too if you seriously want to help someone – Bhumi Shah Dec 14 '15 at 09:23
  • 2
    @BhumiShah No, the code in fiddle is secondary thing, if you add **complete** and **relevant** code in question itself, nobody will ask you to create fiddle. – Tushar Dec 14 '15 at 09:23
  • 1
    http://jsfiddle.net/1q3wnddg/1/ check this. check console – Farhan Dec 14 '15 at 09:25
  • @Farhan : but i am getting p1.TextElement[0].size dynamically so it will not work. – Bhumi Shah Dec 14 '15 at 09:30
  • I didn't get your point about dynamically. but i observed that in console it is undefined for the first time. next time whenever you will run the code it is not – Farhan Dec 14 '15 at 09:33
  • dynamically means this node will exists in each case either dynamically or not. I think point is to selection of current selected item i think... – Farhan Dec 14 '15 at 09:35
  • 1
    Try this? http://jsfiddle.net/xfh8eLyp/ – R Lam Dec 14 '15 at 09:37
  • I have copied code from jsfiddle link.. I hope done now.. – Harsh Baid Dec 14 '15 at 09:56
  • @BhumiShah Check this http://stackoverflow.com/a/4244912/468718 (Your question is also directly related to, in other words duplicate of this question :P) – Harsh Baid Dec 14 '15 at 10:00

1 Answers1

0

property_object_parse is a real JavaScript object, so you can just use the member access syntax to access the value you are interested in directly:

console.log(property_object_parse.p1.TextElement[0].size);

Note that you cannot use a dynamic property path string like 'p1.TextElement[0].size', you would have to compile that in a way. For example, you could instead have an array of properties you are trying to access:

var selected_element = ['p1', 'TextElement', '0', 'size'];
var obj = property_object_parse;
for (var i = 0; i < selected_element.length; i++) {
    obj = obj[selected_element[i]];
}
console.log(obj);

That has the same result as accessing it all directly as above with property_object_parse.p1.TextElement[0].size.

poke
  • 369,085
  • 72
  • 557
  • 602
  • thanks poke, Yeah i got two option: 1. to use eval and 2. to divide into parts but eval is not good to use so i divided into parts like u shown. – Bhumi Shah Dec 16 '15 at 05:36