> 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:
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.
On Friday, July 13, 2012 6:42:14 AM UTC+2, Charles Han wrote:
> 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:
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.
On Friday, 13 July 2012 19:51:16 UTC+12, Yang Guo wrote:
> 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
> On Friday, July 13, 2012 6:42:14 AM UTC+2, Charles Han wrote:
>> 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:
On Fri, Jul 13, 2012 at 1:09 PM, Charles Han
<charleszhouxiao...@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.
On Friday, 13 July 2012 16:42:14 UTC+12, Charles Han wrote:
> 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: