【问题标题】:How to extract data from a json object in android如何从android中的json对象中提取数据
【发布时间】:2016-04-19 09:18:03
【问题描述】:

在我的应用程序中,我收到一个 JSON 对象

{"secQueList":{"1":"你最喜欢哪本书?","2":"你儿时的英雄是谁?","3":"你的宠物叫什么名字?","4 ":"你的第一辆汽车或自行车是什么牌子的?","5":"你最喜欢的颜色是什么?","6":"你最喜欢哪支球队?","7":"叫什么名字你的学校?","8":"你妈妈的娘家姓是什么?","9":"你的出生地是哪里?","10":"你最喜欢的运动是什么?","11":"你最喜欢去哪个地方?"},"que1":null,"ans1":null,"message":null,"fieldErrors":null}

我无法弄清楚我应该如何解析这个对象。 我尝试使用下面的代码,但由于这不是 JSONArray,它会引发异常。

String getParam(String code, String element){
try {
       String base = this.getItembyID(code);
       JSONObject product = new JSONObject(base);
      JSONArray jarray = product.getJSONArray("item");
     String param =  jarray.getJSONObject(0).getString("name");
 return param;
} catch (JSONException e) {
    e.printStackTrace();
    return "error";
}
}

【问题讨论】:

  • 请您的 api 开发人员更改 json。关键应该是修复它不应该像 "1" , "2" 等,而是创建对象数组

标签: android json


【解决方案1】:

我建议您使用 json 格式化程序的站点,它向您显示 json 元素的类型为 http://json.parser.online.fr/ 和 你可以通过 pojo 类使用 Gson 库来解析 json

  public class secQueList {

  public String que1;

  public int ans1;

 public String message;

 public String nextPage;

 public QuestionList secQueList;

 }

public class QuestionList {
  @SerializedName("1")
  public String ques1;

  @SerializedName("2")
  public int ques2;

 @SerializedName("3")
 public String ques3;

 @SerializedName("4")
 public String ques4;

 @SerializedName("5")
 public String ques5;

 @SerializedName("6")
 public String ques6;

 @SerializedName("7")
 public int ques7;

 @SerializedName("8")
 public String ques8;

 @SerializedName("9")
 public String ques9;

 @SerializedName("10")
 public String ques10;

 @SerializedName("11")
 public String ques11;

 }

或者您可以使用内置 JSON 对象进行解析

  String jsonBody = the string you want to parse
  JSONObject quesJsonBody = new JSONObject(jsonBody);
  JSONOBject quesJson = quesJsonBody.getJSONObject("secQueList");
  String quesJson1 =  quesJson.getString("1");
  String quesJson2 =  quesJson.getString("2");
  String quesJson3 =  quesJson.getString("3");
  String quesJson4 =  quesJson.getString("4");
  String quesJson5 =  quesJson.getString("5");
  String quesJson6 =  quesJson.getString("6");
  String quesJson7 =  quesJson.getString("7");
  String quesJson8 =  quesJson.getString("8");
  String quesJson9 =  quesJson.getString("9");
  String quesJson10 = quesJson.getString("10");
  String quesJson11 = quesJson.getString("11");

  String que1 = quesJsonBody.getString("que1");
  String ans1 = quesJsonBody.getString("ans1");
  String message = quesJsonBody.getString("message");
  String fieldErrors = quesJsonBody.getString("fieldErrors");

【讨论】:

    【解决方案2】:
    String base = this.getItembyID(code);
    JSONObject product = new JSONObject(base);
    JSONOBject secQueListJson = product.getJSONObject("secQueList");
    
    // Get all json keys "1", "2", "3" etc in secQueList, so that we can get values against each key.
    Set<Map.Entry<String, JsonElement>> entrySet = secQueListJson .entrySet ();
    
    Iterator iterator = entrySet.iterator ();
    
    for (int j = 0; j < entrySet.size (); j++) {
        String key = null; //key = "1", "2", "3" etc
        try {
            Map.Entry entry = (Map.Entry) iterator.next ();
            key = entry.getKey ().toString ();
          //key = "1", "2", "3" etc
        }
        catch (NoSuchElementException e) {
            e.printStackTrace ();
        }
        if (!TextUtils.isEmpty (key)) {
    
            Log.d ("JSON_KEY", key);
            String value = secQueListJson.getString(key);
            //for key = "1", value = "Which is your favorite book?"
            //for key = "2", value = "Who was your childhood hero?"
        }
    }
    

    【讨论】:

    • 请解释一下?仅由代码组成的答案可能会令人困惑。
    【解决方案3】:

    试试这样的:

    JSONObject jsonObject = new JSONObject(code);
    JSONObject jsonObject1 = jsonObject.getJSONObject("secQueList");
    for (int i=0;i<jsonObject1.length();i++){
     String msg = jsonObject1.getString(String.valueOf(i));
    }
    

    【讨论】:

      【解决方案4】:

      你可以尝试使用 Gson 来解析它。我在类似的情况下在我的应用程序中使用了以下代码:

      ArrayList<String> myArrayList;
      Gson gson = new Gson();
      Type type = new TypeToken<ArrayList<String>>(){}.getType();
      myArrayList= gson.fromJson(code, type);
      

      您必须将 gson 添加到您的 build.grandle 才能使其正常工作

      compile 'com.google.code.gson:gson:2.2.+'
      

      【讨论】:

        【解决方案5】:

        从 JSON 字符串中提取数据

        public void ReadSMS_JSON(String JSON_String) {
                String smsID = "";
                String phone = "";
                String message = "";
        
                JSONObject jsonResponse = new JSONObject(JSON_String);
                JSONArray jsonMainNode = jsonResponse.optJSONArray("tblsms");
                if(jsonMainNode.length()>0){
                    JSONObject jsonChildNode = jsonMainNode.getJSONObject(0);
                    smsID = jsonChildNode.optString("sms_id");
                    phone = jsonChildNode.optString("sms_number");
                    message = jsonChildNode.optString("sms_message");
                }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-09-18
          • 1970-01-01
          • 1970-01-01
          • 2020-10-02
          • 1970-01-01
          • 1970-01-01
          • 2022-01-23
          相关资源
          最近更新 更多