How to access Object content in C++

819 views
Skip to first unread message

Charles Han

unread,
Jul 13, 2012, 12:42:14 AM7/13/12
to v8-u...@googlegroups.com
Hi,

I have managed to get an array back from JavaScript within C++. However, when I got to access the elements of the array, I got something like this:

[object Object]
[object Object]
[object Object]
...

Code:

  Array* uncompressed_json_objects = Array::Cast(*uncompressed_result);
  for(int i =0; i<uncompressed_json_objects->Length(); i++ )
  {
    Local<Object> obj = uncompressed_json_objects->Get(i);
    String::AsciiValue ascii(obj->ToString());
    printf("%s\n", *ascii);
  }

What's the method to get values from a V8 array objects? ()

Thanks

Stephan Beal

unread,
Jul 13, 2012, 3:19:22 AM7/13/12
to v8-u...@googlegroups.com

Your code is correct but Object.toString() (which is what you do with the Ascii part) returns the string you are seeing.

(brevity... phone...)

----- stephan beal
http://wanderinghorse.net/home/stephan/
http://gplus.to/sgbeal

Yang Guo

unread,
Jul 13, 2012, 3:51:16 AM7/13/12
to v8-u...@googlegroups.com
You obviously want to access the object properties. What you currently have is roughly equivalent to this in javascript:

for (var i = 0; i < json_objects.length; i++) {
  var obj = json_objects[i];
  console.log(obj.toString());
}

While what you actually want is

for (var i = 0; i < json_objects.length; i++) {
  var obj = json_objects[i];
  console.log(obj.myProperty.toString());
}

You can use obj->Get(String::New("myProperty")) to get the property.
Or maybe you actually want to print the object using its toString method, which you have not defined yet.

I hope this helps.

Yang

Charles Han

unread,
Jul 13, 2012, 7:09:07 AM7/13/12
to v8-u...@googlegroups.com
Thanks for you reply. I can see what you mean in JavaScript but I don't understand how the data structure in the V8 Array, especially with all the JSON objects in the array.

Can you please explain a bit more?

Thanks

Stephan Beal

unread,
Jul 13, 2012, 7:24:40 AM7/13/12
to v8-u...@googlegroups.com
On Fri, Jul 13, 2012 at 1:09 PM, Charles Han <charleszh...@gmail.com> wrote:
Thanks for you reply. I can see what you mean in JavaScript but I don't understand how the data structure in the V8 Array, especially with all the JSON objects in the array.

Can you please explain a bit more?


An Array object holds arbitrary Value values, and each Value refers to one of the following value types:

- Object
- Array
- Number
- Null
- String
- Undefined
- Function
- Regex

What your code does is extracts a value from the array (the value just happens to be-a Object) and then calls toString() on it. The result of calling toString() on an Object is "[Object object]", which is why you see that output. There is no generic, 1-size-fits-all object-to-string conversion. If you want to output the contents of an Object, you must first fetch those contents from the object or use a generic object-to-string mechanism like JSON.stringify().

On a related note: PLEASE don't use printf() for this type of thing. The v8 example code uses it and is a sign that an absolutely C++ beginner wrote that code (or that whoever wrote it doesn't care much for code quality). In C++ one uses std::cout and std::cerr for this type of simple output, not printf(), and _especially_ when only outputting a string as-is with no formatting. The peformance difference between:

printf("%s\n", "foo")

and:

puts("foo")

is absolutely staggering (the second one is MUCH more efficient), but the end result is identical. printf() has its place, but (printf("%s\n",...)) is ALWAYS a poor choice - puts(...) is faster, uses less stack space, and is not subject to formatting-related injection bugs.

-- 

Charles Han

unread,
Jul 13, 2012, 7:21:10 PM7/13/12
to v8-u...@googlegroups.com
Thanks again for all your answers. I now can print out the objects array content using JSON.stringify.

BTW, are the JSON.parse and JSON.stringify built into the V8 lib? so I don't have to have the javascript file.

Stephan Beal

unread,
Jul 13, 2012, 7:46:11 PM7/13/12
to v8-u...@googlegroups.com
On Sat, Jul 14, 2012 at 1:21 AM, Charles Han <charleszh...@gmail.com> wrote:
BTW, are the JSON.parse and JSON.stringify built into the V8 lib? so I don't have to have the javascript file.

Yes, an RFC-compliant JSON object is built in.

Reply all
Reply to author
Forward
0 new messages