2

可能重复:
在 Android 中发送和解析 JSON

我有JSON以下格式的结果,它JSON Lint显示为Valid Response.

我的问题是:如何访问“reportId0”值“164”、“reportId1”值等都是动态值的157内容reportId2 value 165

我用于访问值的示例代码。properties如何获取值reportid并在其中添加所有值Arraylist

"properties": {    
    "link": "",
    "approvalsReportCount": 3,
    "reportName0": "srcapprovals",
    "reportId0": 164,
    "reportName1": "Approvals",
    "reportId1": 157,
    "requests_report_id": "163",
    "requests_report_name": "EG approvals",
    "reportName2": "fulfillment",
    "reportId2": 165
}
4

4 回答 4

3

这是我发现它获取 ReportId 值的最佳方式。

下面是我的代码

JSONObject jObj = new JSONObject(result);
            JSONObject jsonResultArray = jObj.getJSONObject("results");
            JSONObject pro_object = jsonResultArray.getJSONObject("properties");
            Iterator keys = pro_object.keys();
           while(keys.hasNext()) {
                String currentDynamicKey = (String)keys.next();
                String value = pro_object.getString(currentDynamicKey);
                String upToEightCharacters = currentDynamicKey.substring(0, Math.min(currentDynamicKey.length(), 8));

                if(upToEightCharacters.startsWith("reportId"))
                {

                    Log.v("key"," new report ID key  " + currentDynamicKey);
                    Log.v("key"," new report ID key  " + pro_object.getString(currentDynamicKey) );

                }
              }
于 2012-10-26T08:21:22.713 回答
2

你可以用这个

public ArrayList<String> getReportIds() {
    boolean isContinue = true;
    JSONObject json;
    String tag = "reportId";
    int i = 0;
    ArrayList<String> repIdList = new ArrayList<String>();
    JSONObject prop = null;
    try {
        json = new JSONObject("<your json string>");
        prop = json.getJSONObject("properties");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    while (isContinue) {
        String repId = "";
        try {
            repId = prop.getString(tag + i);
            repIdList.add(repId);
            i++;
        } catch (JSONException e) {
            isContinue = false;
            e.printStackTrace();
        }
    }
    return repIdList;
}
于 2012-10-26T06:22:45.370 回答
1

你可以试试这个!!

try {
               JSONObject jObj = new JSONObject(result);
               JSONObject jsonResultArray = jObj.getJSONObject("results");
               Log.v("log_tag","json result Array :   "+ jsonResultArray);

               JSONObject pro_object = jsonResultArray.getJSONObject("properties");

               Iterator keys = pro_object.keys();

                 while(keys.hasNext()) {
                      // loop to get the dynamic key
                      String currentDynamicKey = (String)keys.next();
                      String value = pro_object.getString(currentDynamicKey);
                      approvaldto_Key = new All_Approval_Key_dto();

                      String upToEightCharacters = currentDynamicKey.substring(0, Math.min(currentDynamicKey.length(), 8));

                      if(upToEightCharacters.startsWith("reportId"))
                      {

                          approvaldto_Key.requestId = pro_object.getString(currentDynamicKey);
                          fetchrecursUserData.add(approvaldto_Key);
                      }

                  }

              }
        catch (JSONException e) {
            e.printStackTrace(); 
            }

          return  fetchrecursUserData;

    }
于 2012-10-26T08:18:06.427 回答
0

你可以试试下面的代码

String serial= jsonObject.getJSONObject("response").getString("serialNumber");

或者

JSONObject json;
try {
    json = new JSONObject(buffer.toString());
    String accessToken = json.getString("access_token");
    return accessToken;
} catch (JSONException e) {
    Log.e("Podcast", "There was an error", e);
}
于 2012-10-26T05:41:40.890 回答