【问题标题】:Parse and Access below JSON data format from HashMap in Java从 Java 中的 HashMap 解析和访问以下 JSON 数据格式
【发布时间】:2019-09-25 09:57:06
【问题描述】:

从 Java 中的 HashMap 解析和访问 JSON 数据格式以下,以验证键与其值的映射。

以下是 JSON 字符串:

{
    "Inputs": {
        "TSVInput": {
            "ColumnNames": ["Bearing_11", "Bearing_12", "Bearing_21", "Bearing_22", "Bearing_31", "Bearing_32", "Bearing_41", "Bearing_42"],
            "Values": [[0.071, -0.054, 0.015, -0.144, -0.227, 0.012, -0.076, -0.073]]
        }
    },
    "GlobalParameters": {}
}

以下源代码在访问特定文件时不进行映射并提供空值。我对如何解析这个嵌套的 JSON 数据感到困惑。

File file = null;
            try {
                BufferedReader br = new BufferedReader(new FileReader(fullFileName));

                String readLine = "";
                System.out.println("Reading file using Buffered Reader");

               while ((readLine = br.readLine()) != null) {
                        //System.out.println(readLine);
                        String jsonString = readline;

                        //System.out.println(jsonString);
                        Gson gson = new Gson();
                        HashMap<String, Object> myMap = gson.fromJson(jsonString, new TypeToken<HashMap<String, Object>>(){}.getType());
                        System.out.println(myMap.get(1));
}

【问题讨论】:

  • myMap 有字符串键并且您正在使用Integer 访问它? - myMap.get(1)
  • 你想从 json 中得到什么值?
  • 您是否要检查键为“Bearing_11”的哈希图中的值是否为 0.071 等等?
  • @pvpkiran 是的,我想检查每个字段的值,我已经修改了代码。

标签: java json hashmap


【解决方案1】:

你可以同时使用 bson 和绕过 hashmap。我很好奇人们如何使用 hashmap 做到这一点。

 //import java.util.ArrayList;
 //import org.bson.Document;

加载你的 json

 Document root = Document.parse("{ \"Inputs\" : { \"TSVInput\" : { \"ColumnNames\" : [\"Bearing_11\", \"Bearing_12\", \"Bearing_21\", \"Bearing_22\", \"Bearing_31\", \"Bearing_32\", \"Bearing_41\", \"Bearing_42\"], \"Values\" : [[0.071, -0.054, 0.015, -0.144, -0.227, 0.012, -0.076, -0.073]] } }, \"GlobalParameters\" : { } }");

您可以将所有 json 对象转换为适当的类型。

 System.out.println(((String)((ArrayList)((Document)((Document)root.get("Inputs")).get("TSVInput")).get("ColumnNames")).get(0)));
 System.out.println(((String)((ArrayList)((Document)((Document)root.get("Inputs")).get("TSVInput")).get("ColumnNames")).get(1)));
 System.out.println(((String)((ArrayList)((Document)((Document)root.get("Inputs")).get("TSVInput")).get("ColumnNames")).get(2)));
 System.out.println(((String)((ArrayList)((Document)((Document)root.get("Inputs")).get("TSVInput")).get("ColumnNames")).get(3)));
 System.out.println(((String)((ArrayList)((Document)((Document)root.get("Inputs")).get("TSVInput")).get("ColumnNames")).get(4)));
 System.out.println(((String)((ArrayList)((Document)((Document)root.get("Inputs")).get("TSVInput")).get("ColumnNames")).get(5)));
 System.out.println(((String)((ArrayList)((Document)((Document)root.get("Inputs")).get("TSVInput")).get("ColumnNames")).get(6)));
 System.out.println(((String)((ArrayList)((Document)((Document)root.get("Inputs")).get("TSVInput")).get("ColumnNames")).get(7)));
 System.out.println(((int)((ArrayList)((Document)((Document)root.get("Inputs")).get("TSVInput")).get("Values")).get(0)));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-06
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多