Hello,
I am trying to figure out how to parse a webservice HTTP POST. The data structure is:
Smith
John
<\names>
Jones
Jack
in Servoy Variable viewer in the debugger:
emp Object
names array[2]
0 {“lastName”:“Smith”,“firstName”:“John”}
1 {“lastName”:“Jones”,“firstName”:“Jack”}
emp.names[0] returns {“lastName”:“Smith”,“firstName”:“John”}
emp.names[0].length() returns 2, so it sees the above data as an object.
emp.names[0].lastName does not work.
How do you parse the object with Javascript?
Thx
Lach
Okay this is how I solved my JSON object problem.
emp.names[0] returns {“lastName”:“Smith”,“firstName”:“John”}
emp.names[0].length() returns 2, so it sees the above data as an object.
emp.names[0].lastName does not work.
var newobj = eval (‘(’ + emp.names[0] + ‘)’);
now…
newobj.lastName returns “Smith”
newobj.firstName returns “John”
thx
Lach
Why the eval?
Why not access emp.names[0].lastName and emp.names[0].firstName directly?
As far as I understand, iterating on emp.names should work fine:
for (var i = 0; i < emp.names.lengh; i++) {
application.output(emp.names[i].lastName + " " + emp.names[0].firstName);
}
Lach,
The eval should not be needed, I will investigate why this happens.
I can already reproduce this from your sample.
Rob
Lach,
This is fixed in next 5.2 build.
The problem is that emp.names[0] returns the string {“lastName”:“Smith”,“firstName”:“John”}.
This should be a js object with a lastName and a firstName.
The eval workaround works now (it converts the string to a js object) but note that after the fix is applied, emp.names[0] will return a js object, so the eval then no longer works.
Rob
Hi Rob,
thx for the update.
I am also having problems with.
In servoy 6.0a2 I can do
var litems = eval(‘(’ + obj.items + ‘)’)
totitems = litems.length
for (var i=0; i<totitems; i++) {
itemid=litems*.itemID*
itemqty=litems*.qty*
…
}
This does not work in 5.2.6 is there a syntax to make it work??
thx
Lach,
With the new code in 5.2 I sent
aaa
9
bbb
99
And could address obj.items.item[0].qty in scripting.
Note that obj.items is not an array, but obj.items.item is
Rob