【问题标题】:Parsing JSON from site to Windows Phone将 JSON 从站点解析到 Windows Phone
【发布时间】:2013-09-04 20:06:33
【问题描述】:

我正在尝试将http://gothere.sg/a/search?q=688609+to+changi+airport&ll=1.362083%2C103.819836 上的 JSON 解析为 Windows Phone 8 应用程序。

目前,我正在使用 Newtonsoft.Json 框架,但并不真正知道如何使用它来解析数据。我也在使用 json2csharp.com 来轻松查看数据。

我需要的数据是Route下的List<string> emailstring route_html,如果我没记错的话。

我将如何使用 Json.NET 或任何其他方法将上述两个解析到我的应用程序中?

编辑:我当前的非工作代码:

        private async void testCode()
    {
        var uri = new Uri("http://gothere.sg/a/search?q=688609+to+changi+airport&ll=1.362083%2C103.819836");
        var client = new HttpClient();

        try
        {
            var response = await client.GetStringAsync(uri);
            JToken token = JObject.Parse(response);
            var token1 = token.SelectToken("route_html").ToString();
            var token2 = token.SelectToken("email").ToString();
        }
        catch (Exception)
        {
        }
    }

【问题讨论】:

  • 首先,这是很多数据,即使使用在线编辑器,也需要筛选很多。我无法轻松找到您要查找的字段。其次,我建议您查看Json.net documentation 的文档。习惯并不难,应该可以消除你的一些困惑。
  • 尝试阅读 Json.net 的文档并使用它,如果仍然无法正常工作,请在此处发布您的尝试,我们会尽力为您提供帮助。
  • 添加了我到目前为止的内容。虽然不确定如何定位特定的类。可能做错了:/

标签: c# json windows-phone-8 windows-phone


【解决方案1】:

如果你使用 json2charp,你会得到很多类。将它们添加到您的项目中。

然后使用以下获取根对象。

RootObject root = JsonConvert.DeserializeObject<RootObject>(data);

然后获取您的列表电子邮件:root.directions.drive.routes.email。

【讨论】:

  • 就是这样!非常感谢! :D
【解决方案2】:

在我看来,Newtonsoft.Json 解析 Json 非常容易。

按照下面的步骤操作

第 1 步: 通过右键单击添加引用来添加服务引用。

步骤 2: 现在将您的 Web 服务链接放在 Service References 上,然后按 go 按钮,并添加服务 Reference 的命名空间

第 3 步: 现在在 .cs 文件中添加 Newtonsoft.Json.Linq; 命名空间

第四步:现在在你的cs文件中添加如下代码

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

第 6 步: 现在生成您的响应方法

 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
                {

                }
            }
        }

希望对你有所帮助。

如果有任何疑问而不是在这里发表评论。我会帮助你

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 2011-10-19
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多