JsonArray in silverlight for javascript

I am creating a JsonArray like:

JsonArray jsonValues = new JsonArray();
 for( int i = 0; i < values.Count; i++ )
 {
   var someSingleValue = values[i];
    jsonValues.Add( string.Format( "Name: {0}", someSingleValue ) );
 }

      

After that I send json values ​​to my javascript in the .aspx page by call: HtmlPage.Window.Invoke ("call", jsonValues);

The call works and it gets there, however I have no idea how to iterate over those values ​​and then retrieve them. I've tried: (in javascript)

for (var x in somevalues){ alert(somevalues); }     

      

I've also tried:

 for(var i = 0; i < somevalues.length; i++) {
            alert(somevalues[i]);
            }

      

but it fails (in both cases) any ideas?

0


a source to share


2 answers


Are you using the eval method to serialize a string for a JSON object?



function call(somevalues){

  //somevalues is currently just a string.
  var results = eval("(" + somevalues +")");

  //results now should contain your array as a JSON object.    

  //and you should be able to iterate over it at this point.
  for(var i = 0; i < results.length; i++){
     alert(results[i]);
  }
}

      

+1


a source


Assuming some of the values ​​are indeed an array, you do it like this:

for(var i = 0; i < somevalues.length; i++) {
   // do something with somevalues[i]
}

      

What you did is tell JavaScript to iterate over the properties of an object somevalues

, which is similar, but not the same as iterating using a regular loop for

.




EDIT: I'm willing to put your variable, somevalues

is it a string. Just do it alert(somevalues)

and find out what happens.

-1


a source







All Articles