Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
How to access Object content in C++
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Charles Han  
View profile  
 More options Jul 13 2012, 12:42 am
From: Charles Han <charleszhouxiao...@gmail.com>
Date: Thu, 12 Jul 2012 21:42:14 -0700 (PDT)
Local: Fri, Jul 13 2012 12:42 am
Subject: How to access Object content in C++

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stephan Beal  
View profile  
 More options Jul 13 2012, 3:19 am
From: Stephan Beal <sgb...@googlemail.com>
Date: Fri, 13 Jul 2012 09:19:22 +0200
Local: Fri, Jul 13 2012 3:19 am
Subject: Re: [v8-users] How to access Object content in C++

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
On Jul 13, 2012 6:42 AM, "Charles Han" <charleszhouxiao...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Yang Guo  
View profile  
 More options Jul 13 2012, 3:51 am
From: Yang Guo <yang...@chromium.org>
Date: Fri, 13 Jul 2012 00:51:16 -0700 (PDT)
Local: Fri, Jul 13 2012 3:51 am
Subject: Re: How to access Object content in C++

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Charles Han  
View profile  
 More options Jul 13 2012, 7:09 am
From: Charles Han <charleszhouxiao...@gmail.com>
Date: Fri, 13 Jul 2012 04:09:07 -0700 (PDT)
Local: Fri, Jul 13 2012 7:09 am
Subject: Re: How to access Object content in C++

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stephan Beal  
View profile  
 More options Jul 13 2012, 7:24 am
From: Stephan Beal <sgb...@googlemail.com>
Date: Fri, 13 Jul 2012 13:24:40 +0200
Local: Fri, Jul 13 2012 7:24 am
Subject: Re: [v8-users] Re: How to access Object content in C++

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.

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Charles Han  
View profile  
 More options Jul 13 2012, 7:21 pm
From: Charles Han <charleszhouxiao...@gmail.com>
Date: Fri, 13 Jul 2012 16:21:10 -0700 (PDT)
Local: Fri, Jul 13 2012 7:21 pm
Subject: Re: How to access Object content in C++

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stephan Beal  
View profile  
 More options Jul 13 2012, 7:46 pm
From: Stephan Beal <sgb...@googlemail.com>
Date: Sat, 14 Jul 2012 01:46:11 +0200
Local: Fri, Jul 13 2012 7:46 pm
Subject: Re: [v8-users] Re: How to access Object content in C++

On Sat, Jul 14, 2012 at 1:21 AM, Charles Han
<charleszhouxiao...@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.

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »