【问题标题】:Accessing json string in java and creating hashmap Android在java中访问json字符串并创建hashmap Android
【发布时间】:2014-10-13 13:34:53
【问题描述】:

我有一个 JSON 字符串,我正在尝试从中检索信息。 Json 字符串看起来像这样。

JSON 字符串:

{
    "information": {
        "device": {
            "id": 0
        },
        "user": {
            "id": 0
        },
        "data": [
            {
                "datum": {
                    "id": "00GF001",
                    "history_id": "9992BH",
                    "name": "abc",
                    "marks": 57,
                    "class": "B",
                    "type": "Student"
                }
            },
            {
                "datum": {
                    "id": "72BA9585",
                    "history_id": "78NAH2",
                    "name": "ndnmanet",
                    "marks": 70,
                    "class": "B",
                    "type": "Student"
                }
            },
            {
                "datum": {
                    "id": "69AHH85",
                    "history_id": "NN00E3006",
                    "name": "kit",
                    "department": "EF003",
                    "class": "A",
                    "type": "Employee"
                }
            },
            {
                "datum": {
                    "id": "09HL543",
                    "history_id": "34QWFTA",
                    "name": "jeff",
                    "department": "BH004",
                    "class": "A1",
                    "type": "Employee_HR"
                }
            }
        ]
    }
}

我正在尝试从中访问数据 JSONArray 和相应的 Datum。我根据学生、员工等类型区分每个数据,并在哈希图中推送信息。 我在 javascript 中成功地做到了,但在 Java 中我很挣扎。

当我尝试访问 JSONArray 时,它会抛出异常

 try {
            JSONObject data = new JSONObject(dataInfo);
           // Log.d(TAG, "CHECK"+data.toString());
         JSONObject info = data.optJSONObject("information");
            if(info.getJSONArray("data").getString(0).equals("Student") > 0)  //exception here

                Log.d(TAG, "Data"+ data.getJSONArray("data").length()); //exception here too
            for(int m = 0; m < data.length(); m++){
                //   for(int s = 0; s < data[m].ge)
            }
        } catch (JSONException j){
            j.printStackTrace();
        }

我有任何用于创建相应类型的 hashmap 的指针。赞赏

【问题讨论】:

  • 您提供的代码没有足够的信息来完全帮助您。例如,current 是什么?除非我遗漏了什么,否则您要返回的 JSON 中没有 "signals"
  • 对不起,我已经编辑了,但是如何从这个 json 字符串访问类型

标签: java android json hashmap


【解决方案1】:

如果您尝试访问 datum 对象的 type 字段,您需要这样的内容:

JSONObject data = new JSONObject(dataInfo); // get the entire JSON into an object
JSONObject info = data.getJSONObject("information"); // get the 'information' object
JSONArray dataArray = info.getJSONArray("data"); // get the 'data' array

for (int i = 0; i < dataArray.length(); i++) {
    // foreach element in the 'data' array
    JSONObject dataObj = dataArray.getJSONObject(i); // get the object from the array
    JSONObject datum = dataObj.getJSONObject("datum"); // get the 'datum' object
    String type = datum.getString("type"); // get the 'type' string

    if ("Student".equals(type)) {
        // do your processing for 'Student' here
    }
}

请注意,您必须处理异常处理、错误数据等。这段代码只是向您展示了如何获取您正在寻找的数据的基础知识。我将每个单独的步骤分成了自己的代码行,以便我可以清楚地注释每个步骤发生的情况,但如果这对您来说更容易,您可以将一些步骤组合成一行代码。

【讨论】:

  • 谢谢我在遵循 json origin json String 的层次结构时有点困惑和混乱。谢谢
【解决方案2】:

如果dataInfo是你发布的json,那么你必须访问信息,从信息中,你可以访问数据:

 JSONObject data = new JSONObject(dataInfo);
 JSONObject info = data.optJSONObject("information");
 if (info != null) {
    JSONArray dataArray = info.optJSONArray("data")
 }

【讨论】:

    猜你喜欢
    • 2014-03-27
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    相关资源
    最近更新 更多