Deamination from JSON (ScriptObject) to managed entity

I am trying to deserialize the json returned from javascript via Silverlight.

Mostly on the client side, I return JSON and in my C # handler, I get it through ScriptObject ...

I tried the ConvertTo method in ScriptObject and still could not get anything.

How can I convert a ScriptObject to a C # object, which is a list of objects?

SomeCallBack(ScriptObject result) {

    // convert to managed object

    var objects = result.ConvertTo<List<SomeObjectClass>>(); // can't get any property from it..

    // however the count is correct...
    MessageBox.Show("count: " + objects.Count); // shows correct count of items
}
+3
source share
4 answers

Silverlight does not contain an API for wrapping ScriptObjectand serializing to a JSON string.

Silverlight supports JSON serialization through a class System.Runtime.Serialization.Json.DataContractJsonSerializerfound in the dll System.ServiceModel.Web.

JSON- Javascript , ScriptObject, JSON ScriptObject. , JQuery.

, (JSON "[x1, x2,, xn]" ), x SomeObjectClass. : -

    List<T> DeserializeJSON<T>(string json)
    {

        byte[] array = Encoding.UTF8.GetBytes(json);
        MemoryStream ms = new MemoryStream(array);

        DataContractJsonSerializer dcs = new DataContractJsonSerializer(typeof(List<T>));
        return (List<T>)dcs.ReadObject(ms);

    }

: -

 var objects = DeserializeJSON<SomeObjectClass>(someJSON);
+1

http://json.codexplex.com/

Silverlight, Newtonsoft.Json.Linq.JObject - , .

+1

JSON, ScriptObject.ConvertTo JSON #. , :

JSON

{ id: 0001,
  name: 'some_name',
  data: [0.0, 1.0, 0.9, 90.0] }

#

using System.Runtime.Serialization;  // From the System.Runtime.Serialization assembly

[DataContract]
public struct JsonObj
{
    [DataMember]
    public int id;

    [DataMember]
    public string name;

    [DataMember]
    public double[] data;
}

#

public void SomeCallback(ScriptObject rawJsonObj)
{
    // Convert the object
    JsonObj jsonObj = rawJsonObj.ConvertTo<JsonObj>();
}

, , JSON, JSON, , . . MSDN.

, .

0

JS C#. , JS (. Collection , - - , JS !).

SL Code ( JS SL ):

[ScriptableMember()]
public string GetValue(ScriptObject o)
{

  Prototype p = (Prototype)o.ConvertTo<Prototype>();
  return "A";
}

JS:

control.Content.ExternalName.GetValue({ Collection: [{ a: "A1" }, { a: "A2"}] })

#

public class Prototype
{  
  public List<PrototypeItem> Collection
  {
    get;
    set;
  }
}

public class PrototypeItem
{
  public string a
  {
    get;
    set;
  }
}
0

Source: https://habr.com/ru/post/1790873/


All Articles