c# - Json Serialization for Windows Phone -


i trying implement parsing json response shown here windows phone 7 project in c#. stuck compilation error "the type or namespace name 'serializable' not found (are missing using directive or assembly reference?)"

i have imports using system.runtime.serialization; using system.runtime.serialization.json; not sure import missing. tried include using system.servicemodel.web; web part not recognized.

i thought project not pointing right framework here. in assembly information, there no option me change target framework.

this looks similar problem mine, couldn't find json.net in .net dlls filtered windows phone.

can me json thing working windows phone 7.

thank in advance.

edit - 7/3/11

my jason response { "serviceresponse" : { "servicename" : "requestregisteruser", .....

and response objects are:

[datacontract]        public class serviceresponse        {              [datamember]            public string servicename { get; set; }            .            .            . 

and deserialize method:

 public static t deserialise<t>(string json)    {        t obj = activator.createinstance<t>();        using (memorystream stream = new memorystream(encoding.unicode.getbytes(json)))        {            datacontractjsonserializer serializer = new datacontractjsonserializer(typeof(t));            obj = (t)serializer.readobject(stream);            return obj;        }    } 

now getting error after deserializing response: servicename not evaluate expression string

( not import system.servicemodel.web though have dll in reference. compilation error on .web part (the type or namespace name 'web' not exist in namespace 'system.servicemodel') )

edit after more research, found response when viewed in debugger { \"serviceresponse\": { \"servicename\": \"requestregisteruser\",..... searched , found problem. how can format correct json string??

you need add reference both system.runtime.serialization , system.servicemodel.web assemblies. datacontractjsonserializer defined in system.servicemodel.web assembly in silverlight version of framework, that's why need assembly reference.

and way json.net a popular open-source json framework .net , find more here. it's not part of .net framework, that's why can't find it.

edit:

  1. about compilation, in silverlight datacontractjsonserializer in system.runtime.serialization.json namespace, in assembly system.servicemodel.web (in system.servicemodel.web.dll), bit confusing. use - system.runtime.serialization.json.datacontractjsonserializer, need assembly reference. need reference system.runtime.serialization assembly well, because datacontract attribute defined. see have compiled code, hope explanation makes more clear future readers.
  2. about serialization - have found out, need 2 objects, because that's structure of json. however, datacontract , datamember attributes have name property can use instead of changing name of fields. also, can use properties instead of fields if like.

for example:

[datacontract] public class serviceresponse {     [datamember(name = "servicename")]     public string servicename { get; set; } }  [datacontract] class response {     [datamember(name = "serviceresponse")]     public serviceresponse serviceresponse { get; set; } } 

and 1 last thing - don't need call activator.createinstance(); in deserialise method.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -