【问题标题】:How to get the next value from a JSONObject如何从 JSONObject 获取下一个值
【发布时间】:2018-05-08 23:22:41
【问题描述】:

我从 .php 文件中获得了这些信息:

"{"limitacao_motora":[{"descricaoLimitacao":"Wheelchair"}]}
 {"limitacao_motora":[{"descricaoLimitacao":"Crutches"}]}
 {"limitacao_motora":[{"descricaoLimitacao":"Walk short distances"}]}
 {"limitacao_motora":[{"descricaoLimitacao":"Only short stairs"}]}
 {"limitacao_motora":[{"descricaoLimitacao":"Only short stairs"}]}"

我需要将每个选项分离到 android studio 中的 textView 中。但我只能得到名字“Wheelchair”。

我的获取信息的代码是:

JSONObject reader2 = new JSONObject(resp);
final JSONObject limitacao_motora =
        (JSONObject) reader2.getJSONArray("limitacao_motora").get(0);

并将信息放入textView是:

((TextView) findViewById(R.id.limi))
        .setText(limitacao_motora.getString("descricaoLimitacao"));"

我的输出是带有单词“wheelchair”的 textView “limi”。 现在我需要将“拐杖”放入其他 textView 但我不知道如何。

我是这方面的新手,我搜索过但我不明白我的问题。

提前致谢。

【问题讨论】:

  • 请创建您的数据 json 接受.. 我认为数据是错误的。我尝试使用 jslint 进行验证,结果是错误的。 -->imgur.com/jonDShI

标签: java php arrays json


【解决方案1】:

正如@muhammadaa 所说,你的 json 有点奇怪。
你有能力调整你的 json 响应吗? 如果答案是肯定的,请看看这个替代模型:

{
   "limitacao_motora": true,
   "descricaoLimitacao": ["Wheelchair", "Crutches", "Walk short 
    distances", "Only Short stairs"]
}

通过这个 JSON 响应,您可以知道条件 limitacao_motora 是否适用于您当前的对象,并且您可以像这样检索您的 descricaoLimitacao 数组值:

//Retrieve your array
JSONArray descricoesLimitacoes = yourJsonResponse.getJSONArray("descricaoLimitacao");

准备好 JSONArray 数组对象后,您可以调用 get 方法传递数组索引值来检索其值。

所以,我希望这个答案能给你一些澄清和想法,以改进你的代码实现:)

如果您有任何问题,请告诉我,以便我可以帮助您解决!

【讨论】:

    猜你喜欢
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 2017-10-02
    • 2020-03-01
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    相关资源
    最近更新 更多