PDA

View Full Version : JSON-RPC and Ext.util.JSON.decode...???



madmike2k
12 Mar 2010, 7:07 AM
Hi,

I have a ASP.NET C# WebService with Jayrock with a method like this:


public List<Field> GetFields()
{
....
}
public class Field
{
public Field(string xPosition, int xLeft, int xTop, int xLength, string xAttributeBase, string xProtected, string xFieldType, string xText)
{

public string Position
{

public int Left
{

public int Top
{

public int Length
{

public string AttributeBase
{
get { return _AttributeBase; }
set { _AttributeBase = value; }
}

public string Protected
{

public string FieldType
{

public string Text
{
}


When I invoke this method I get simething like this:


[{"position":"1","left":1,"top":0,"length":4,"attributeBase":"206","protected":"true","fieldType":"High","text":"abc"},{"position":"6","left":6,"top":0,"length":15,"attributeBase":"192","protected":"false","fieldType":"","text":"def"},{"position":"22","left":22,"top":0,"length":23,"attributeBase":"206","protected":"true","fieldType":"High","text":"ghi"}]


Now my client code call this method and get the response:


var s = new Service();
s.GetFields(function(response){
var fields = Ext.util.JSON.decode(response);
});


Now I try something like this: fields[0].position but it won't work....
How can I iterate over the fields?
Any ideas?

Thanks a lot!

Greetings

Mike Robinson
12 Mar 2010, 7:25 AM
Start by looking at the variable, e.g. in Firebug, to make sure you know what you've actually got.

If you do indeed have an "array of hashes," as you think you should, then you can iterate across it in any number of ways. "Ext.each()," a for-loop ... But until you know what the variable you decoded actually looks like, you could be spinning your wheels.

Set a breakpoint on the code and walk through it step-by-step once. It is painfully easy to write code that does not generate a syntax error but that also does not do what you want it to. Which is a huge shock compared to strongly typed languages like C# or VB (ASP).

madmike2k
12 Mar 2010, 11:21 AM
@Mike Robinson
Thanks a lot!
I don't need a "Ext.util.JSON.decode(response)"... =P~ It's enough when I call "response.result[0].text" directly... So a parsing to a JSON object is not needed, I have already a working JSON object in my "response.result". =D>

Greetings