【问题标题】:Putting JSON values into Hashmap将 JSON 值放入 Hashmap
【发布时间】:2015-11-25 10:57:12
【问题描述】:

我有如下 JSON 值,

   { "emp_id": 1017,
   "emp_name": "karthik Y", 
  "emp_designation": "Manager", 
   "department": "JavaJson", 
   "salary": 30000, 
   "direct_reports":
  [ 
  "Nataraj G",
   "Kalyan", 
  "Mahitha" 
  ] 
} 

 HashMap < String, String[] >input1 = new HashMap < String, String[] >();
 input1.put("empid","1017");
 input1.put("emp_name","karthik");
 input1.put("emp_designation","manager");
 input1.put("salary","30000");

现在我想添加下一个数组,即 direct_report 作为下一个键和值(整个数组应该是一个键和值)。有人请帮忙。

【问题讨论】:

  • 键和值必须是什么?
  • 映射 dbInputMap = new HashMap(); dbInputMap.put("Parm1", new String[]{emp_id+ ""}); dbInputMap.put("Parm2", new String[]{emp_name + ""}); dbInputMap.put("Parm3", new String[]{emp_designation+ ""}); dbInputMap.put("Parm4", new String[]{department+ ""}); dbInputMap.put("Parm5", new String[]{salary+ ""}) 我也想把直接报告数组放到这张地图上,我已经把这张地图传到了某个地方。

标签: java arrays json


【解决方案1】:

Hashmap 是一种键/值存储,其中键是唯一的。您可以将 JSON 转换为字符串,然后将其作为值存储到 hashmap。比如下面这样:

public static void main(String[] args) {
        String json = "{ \"emp_id\": 1017," 
               + "\"emp_name\": \"karthik Y\"," 
               + "\"emp_designation\": \"Manager\"," 
               + "\"department\": \"JavaJson\"," 
               + "\"salary\": 30000," 
               + "\"direct_reports\": [" 
               + "\"Nataraj G\","
               + "\"Kalyan\"," 
               + "\"Mahitha\"]}"; 

        HashMap<String, String> jsonStore = new HashMap<String, String>(); 
        jsonStore.put("myJson", json); 

        System.out.println(jsonStore.get("myJson"));
    }

你需要也可以使用'org.json'库来

  • 手动创建 JSON 对象
  • 将现有 JSONObject 转换为字符串表示形式
  • 将 JSON 字符串转换为 JSONObject

您还可以有以下解决方案:

JSONObject jsonObject = new JSONObject(); 
jsonObject.put("empt_id", 1017); 
jsonObject.put("emp_name", "karthik"); 

HashMap<String, JSONObject> jsonObjectStore = new HashMap<String, JSONObject>(); 
jsonObjectStore.put("myJsonObject", jsonObject); 

HashMap<JSONObject, String> jsonObjectStore2 = new HashMap<JSONObject, String>();
jsonObjectStore2.put(jsonObject, "myJson"); 

确保您下载了 org.json jar 文件并将其放在您的类路径中,以便能够使用 JSONObject。你可以从here下载jar。

为了将这些值中的每一个作为单个键/值条目放入映射中。您自己已经提到过,它应该可以正常工作。见以下方法:

方法一 Java 中的一切都是 Object,String 继承 Object,String[] 继承 object。您可以有以下解决方案:

HashMap<String, Object> myObjectStore4 = new HashMap<String, Object>();

String[] directReports4 = new String[]{"Natraj G", "Kalyan", "Mahitha"}; 

myObjectStore4.put("emp_id", new String("123")); 
myObjectStore4.put("emp_name", new String("Raf")); 
// others .... 
myObjectStore4.put("directReports", directReports4); 

方法二 要将字段存储为键/值,并且如果您负担得起将数组转换为字符串(表示所有数组元素以逗号分隔,则使用此方法)。

HashMap<String, String> myObjectStoreTwo = new HashMap<String, String>();

String[] directReports2 = new String[]{"Natraj G", "Kalyan", "Mahitha"}; 

myObjectStoreTwo.put("emp_id", "123"); 
myObjectStoreTwo.put("emp_name", "Raf"); 
myObjectStoreTwo.put("salary", "222");

//Converts array to comma separated String 
myObjectStoreTwo.put("directReports",Arrays.toString(directReports2));

方法三 以使用 Hash Map 存储 String 键和 Array 值为代价。您也必须将其他元素作为数组。

HashMap<String, String[]> myObjectStore3 = new HashMap<String, String[]>();

String[] directReports3 = new String[]{"Natraj G", "Kalyan", "Mahitha"}; 

myObjectStore3.put("emp_id", new String[]{123 + ""});
myObjectStore3.put("salary", new String[]{32312 + ""}); 
myObjectStore3.put("directReports", directReports3);

【讨论】:

  • Direct_report 键包含一组数组值。如何将一组数组值设置为单个键和值。
  • 要正确理解你的直接报告是这样的 > String[] directReports = {"Natraj G", "Kalyan", "Mahitha"};正确的?如果是,那么您可以轻松地将其添加到您在问题中提到的 Map 中。
  • 我已经修改了我的问题,再看一遍。
  • 有几种方法可以做到,我将更新答案以列出所有方法。
【解决方案2】:

使用杰克逊 ObjectMapper。试试看是否可行

String json = "{....}"
HashMap<String,Object> mappedVals = new ObjectMapper().readValue(
                    json ,
                    new TypeReference<HashMap<String,Object>>() {
                    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 2013-01-12
    • 1970-01-01
    • 2013-10-31
    相关资源
    最近更新 更多