【问题标题】:how can i retrieve the json data from url and store it in an array..?如何从 url 检索 json 数据并将其存储在数组中..?
【发布时间】:2013-11-07 11:38:17
【问题描述】:

我正在为 windows phone 8 工作..
我需要从 url 检索 Json 数据,我需要在数组中显示..
我的网址有这样的jsondata..

 {       id: 9
            address: "abc"
            city: " chennai"
            contact1: ""
            contact2: ""
            country: " india"
            description: ""
}

我想知道如何检索这些数据并将其存储在数组中?

这是我用来检索 Json 数据的语句。

private string REST_URL = "my url"; //i'm using my url here to extract the json data..

    String servicePath = REST_URL + "/data/" + query;            
    Detail[] detail = JsonConvert.DeserializeObject<Detail[]>(servicePath);
    return detail;

我的完整 Json 数据

[
  {
    "id": 01,
    "address": "12asdf",
    "city": " chennai",
    "contact1": "",
    "contact2": "",
    "country": " india",
    "description": "",
    "name": " david",
    "region": "",
    "state": "  033",
    "website": "",
    "image": "",
     "PrayerTime": {
      "id": 01,
      "PrayerTime1": "00:52",
      "PrayerTime2": "21:04",
      "PrayerTime3": "12:27",
      "PrayerTime4": "05:35",
      "PrayerTime5": "21:04",
      "created_at": null,
      "PrayerTime6": "04:01",
      "updated_at": null,
      "organization_id": 001
    }
  },.............
  }

【问题讨论】:

  • 您遇到了什么问题?您发布的 JSON 不是数组,而是单个对象。
  • 我需要将它存储到一个数组中......所以我该怎么做......?或者帮助我从 url 检索 json 数据并显示它..
  • 你能发布整个 JSON 吗?如果有问题的 JSON 正确,则仅使用 Detail 而不是 Detail[]
  • 我已经更新了完整的 Json 数据..
  • 看来您的 JSON 可能是错误的。使用 json2csharp.com 从 JSON 创建类。

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


【解决方案1】:

首先确保您的模型是正确的。正如@Xyroid 所说,使用json2csharp.com,它允许您简单地复制和粘贴您的json,它会生成相应的c# 代码。

您不能将 URL 提供给解析器,您首先必须将 JSON 作为字符串下载。要下载字符串,只需使用HttpClient,您甚至可以在PCL 中使用它。

var httpClient = new System.Net.Http.HttpClient();
string jsonData = httpClient.GetStringAsync(url);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-01
  • 1970-01-01
相关资源
最近更新 更多