【问题标题】:Json Parsing, need to get key name from response not key valueJson Parsing,需要从响应中获取键名而不是键值
【发布时间】:2016-05-19 04:07:26
【问题描述】:

Json 响应:

response :{"status":false,"Message":"Some Database Error"}

  • 要求:

我想要键名,即消息而不是消息的值

代码:

private final String KEY_MSG = "Message";
private final String KEY_MSG1 = "message";

try {
            AppLog.Log("TAG123", response);
            JSONObject jsonObject = new JSONObject(response);
            String message1="Message";
            if (message1.equals(jsonObject.getString(KEY_MSG)))
            {
                AppLog.Log("fgesarfefe", "dsfdsfdsfds");
                return jsonObject.getString(KEY_MSG);

            }
            else
            {
                AppLog.Log("00000000", "111111");
                return jsonObject.getString(KEY_MSG1);
            }
           /* if (jsonObject.getString(KEY_MSG).equals("message"))
            {
                return jsonObject.getString(KEY_MSG1);
            }
           else
            {*/
              //  return jsonObject.getString(KEY_MSG);
           // }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "No data";

【问题讨论】:

    标签: json parsing android-studio response


    【解决方案1】:

    您可以使用JSONObject.keySet() 方法来获取该对象上的一组键。在您的情况下,它应该返回 "status""Message"

    如果你想获得单独的密钥,你可以使用迭代:

    for (String jsonKey : jsonObject.keySet()) {
         // Check each key here, jsonKey value will be "status" and "Message"
    }
    

    希望对你有帮助,祝你好运

    【讨论】:

      猜你喜欢
      • 2020-03-11
      • 2017-04-29
      • 1970-01-01
      • 2020-06-25
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多