【问题标题】:Android json parse with Retrofit library使用 Retrofit 库进行 Android json 解析
【发布时间】:2017-04-14 22:35:53
【问题描述】:

我找到了以下 json 格式的文件。是否可以通过改造进行解析?通常 json 对每个部分都有 key 和 value 但这里一个数组有 key 而其他数组有 values 。那么如何解析呢?

{
  "insertnewdatas": {
    "columns": [
      "id",
      "operatorName",
      "latitude",
      "longitude",
      "location",
      "address",
      "currencies",
      "exchange",
      "exchangeLimits",
      "workingDays",
      "link",
      "phone",
      "email",
      "dateTime",
      "created_at",
      "updated_at"
    ],
    "records": [
      [
        3,
        "Coin Source",
        "22.3475",
        "91.8123",
        "Chittagong",
        "Oxygen , Chittagong",
        "Cash",
        "option2",
        "$500/month",
        "30",
        "no link",
        "017000000000",
        "arifhasnat.info@gmail.com",
        "21-10-2017",
        "2017-03-01 17:40:42",
        "2017-03-01 17:40:42"
      ],
      [
        4,
        "Coin Source",
        "22.3475",
        "91.8123",
        "Chittagong",
        "Oxygen , Chittagong",
        "Cash",
        "option2",
        "$500/month",
        "30",
        "no link",
        "017000000000",
        "arifhasnat.info@gmail.com",
        "21-10-2017",
        "2017-03-01 17:40:49",
        "2017-03-01 17:40:49"
      ],
      [
        5,
        "Bit Coin",
        "22.3575",
        "91.8323",
        "Chittagong",
        "Chittagong",
        "both",
        "option1",
        "$500/month",
        "30",
        "no link",
        "017000000000",
        "arifhasnat.info@gmail.com",
        "21-10-2017",
        "2017-03-01 17:43:34",
        "2017-03-01 17:43:34"
      ]
    ]
  }
} 

【问题讨论】:

  • 这是一种非常糟糕的格式。我建议您先尝试更改它

标签: android json rest asp.net-web-api retrofit2


【解决方案1】:

默认情况下,Retrofit 不支持自动 JSON 解析。但是您可以为此使用 GSON。 https://github.com/google/gson

关于如何使用 Retrofit 和 GSON 解析 JSON 的简短教程,您可以在这里找到例如:http://www.pratikbutani.com/2016/05/android-tutorial-json-parsing-using-retrofit-part-1/

但是还有更多的教程可以帮助你。

【讨论】:

  • 我知道 json 解析与改造,但这种格式是可能的?有这种格式的例子吗?
猜你喜欢
  • 2016-01-11
  • 2019-01-19
  • 2015-04-07
  • 2021-04-24
  • 2020-06-07
  • 1970-01-01
  • 2018-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多