How to convert an object to JSON data in windows phone. In web application I have used following code

 JavaScriptSerializer serializer = new JavaScriptSerializer();
 string stringData = serializer.Serialize(object);

I want to get the same output as that of the above code in windows phone 7.

有帮助吗?

解决方案

JavaScriptSerializer is not supported on Windows Phone. An alternative is to use JSON.NET (you can add it via NuGet).

The code will then look like this:

string stringData = JsonConvert.SerializeObject(object);

其他提示

firrst you need to download Newtonsoft.Json dll for parse web serevice

Just follow bellow step

Step1: Add Service References by right click on add References.

Step2: Now put your web service link on Service References and press go button, And also add Namespace of service Reference enter image description here

Step3: Now add using Newtonsoft.Json.Linq; name space in your .cs file

Step4: Now add bellow code in your cs file

 WhatsupServices.WhatsUpServiceSoapClient ws = new WhatsupServices.WhatsUpServiceSoapClient();
ws.ContactUsJSONCompleted += ws_ContactUsJSONCompleted;
ws.ContactUsJSONAsync(txtContactUsName.Text, txtContactUsPhone.Text, txtContactUsEmail.Text, txtContactUsComment.Text);

step6: now genrate your resopnce method

 void ws_ContactUsJSONCompleted(object sender, dynamic e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(LogIn.NetworkBusyMsg, LogIn.MsgHdr, MessageBoxButton.OK);
                busyIndicator.IsRunning = false;
            }
            else
            {
                busyIndicator.IsRunning = false;
                string Result = e.Result;
                JObject obj = JObject.Parse(Result);
                string ResultCode = (string)obj["ResultCode"];
                string ResponceMessage = (string)obj["ResponseMessage"];

                if (ResultCode == "1")
                {
                    MessageBox.Show("Thank you for your message. We'll get back to you soon.", LogIn.MsgHdr, MessageBoxButton.OK);
                    NavigationService.GoBack();
                }
                else
                {

                }
            }
        }

Hope it will help you.

If any query than comment here.I wll help you

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top