一尘不染

Android应用程序中的Json解析

json

现在完成XML SAX解析之后,我将在应用程序中进行JSON解析。我在这里为您提供杰森

Response : {"menu": {
              "id": "file",
              "value": "File",
              "popup": {
                "menuitem": [
                  {"value": "New", "onclick": "CreateNewDoc()"},
                  {"value": "Open", "onclick": "OpenDoc()"},
                  {"value": "Close", "onclick": "CloseDoc()"}
                ]
              }
            }}

现在,我提到了一个Json解析的示例,在该示例中,他们创建了一个jString对象,我对此特定行有疑问,如下所示:

private String jString = "{\"menu\":    {\"id\": \"file\", \"value\": \"File\", \"

任何人都可以让我澄清一下。完整示例的链接如下:

http://www.androidcompetencycenter.com/2009/10/json-parsing-in-
android/


阅读 204

收藏
2020-07-27

共1个答案

一尘不染

您可以像这样从响应中获取JSON对象(我假设您知道如何处理响应):

String[] file = null;

/* Make a JSON object from your response, yourResponse is a String containing
the whole response */
JSONObject jsonObject = new JSONObject(yourResponse);

/* Your "menu": array from your response */
JSONArray infoArray = jsonObject.getJSONArray("menu");

/* If the array contains multiple items, loop it
through the infoArray's size */
for (int i = 0; i < infoArray.length(); i++) {  
        try {    
          //Get the value inside "id"
          file[i] = infoArray.getJSONObject(i).optString("id");                  
            } catch (JSONException e) {
            }       
}
2020-07-27