JSON

Silverlight passing JSON object incorrectly?

字号+ 作者:H5之家 来源:H5之家 2017-08-06 17:00 我要评论( )

Silverlight passing JSON object incorrectly?-程序代码库-The program code library

收录日期:2017/08/06 17:01:10 时间:2010-02-19 11:26:04 标签:javascript,silverlight,json,serialization,silverlight-3.0

I have a Silverlight class marked with the ScriptableType & ScriptableMember and I expect to be able to pass the object from Silverlight to javascript. When I call JSON.stringify (in javascript) I expect to receive a JSON representation of the object but all I get is {}

The class is defined as:

[ScriptableType()] public class MyEvent { [ScriptableMember(ScriptAlias = "eventContent")] public int EventContent { get; set; } }

I pass the object from Silverlight like this:

var jsonObject = new MyEvent { EventContent = 1 }; HtmlPage.Window.Invoke("publishValue", topic, jsonObject);

And in javascript I'm doing the following:

alert(topic); alert(jsonObject); alert(JSON.stringify(jsonObject));

When I use the debugger I only see the jsonObject as of type Object but the call alert(jsonObject) returns the correct type and if I access the property jsonObject.eventContent I get the correct value back, but it doesn't serialize correctly with JSON.stringify.

Anyone tell what I'm doing wrong?

I don't want to have to serialize the object in Silverlight before sending to javascript.

Cheers

AWC

JSON.stringify enumerates over enumerable properties of an object using for...in. If the properties aren't enumerable, they won't be included in the resulting string.

The Silverlight object is an external object, not a native javascript object. Just like an ActiveXObject, the properties are not discoverable/enumerable. I'm not sure if there's a way around this. A couple of pages I found hint towards implementing IEnumerable to be able to iterate using a foreach in the native language but I'm not sure if this would carry over to JavaScript.

I wouldn't rely on it being possible but you never know. If you require an object to be enumerable, the only way might be to serialize it using System.Json and call eval on the document to unserialize it in JavaScript.

I've managed to solve the problem!

Instead of declaring an object like this for passing from Silverlight to javascript:

[ScriptableType()] public class MyEvent { [ScriptableMember(ScriptAlias = "eventContent")] public int EventContent { get; set; } }

I use the System.Json namepsace and create a JsonObject like this:

var ob = new JsonObject { {"eventContent", 1} };

Check out the documentation of System.Json.JsonObject for more info.

Cheers

AWC

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • JSON与JS对象的区别与对比

    JSON与JS对象的区别与对比

    2017-08-06 16:00

  • jquery解析json拼接到下拉框(含jar包)

    jquery解析json拼接到下拉框(含jar包)

    2017-08-05 16:01

  • PHP json_decode遇到的坑

    PHP json_decode遇到的坑

    2017-08-05 16:01

  • 织梦标签:json JSON数据调用

    织梦标签:json JSON数据调用

    2017-08-05 14:00

网友点评