java - 将 JSON 值放入 Hashmap

标签 java arrays json

我的 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 来作为下一个键和值(整个数组应该是一个键和值)。请有人帮忙。

最佳答案

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包.

为了将每个值作为单个键/值条目放入映射中。您自己已经提到过,它应该可以正常工作,没有任何问题。请参阅以下方法:

方法1 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); 

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

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));

方法3 以 HashMap 来存储字符串键和数组值为代价。您也必须将其他元素作为数组放置。

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);

关于java - 将 JSON 值放入 Hashmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33914690/

相关文章:

c++ - 初始化结构数组

javascript - 为什么在创建对象的新实例时函数的属性没有被覆盖为默认值?

java - 具有完整 POJO 数据绑定(bind)的 Jackson 自定义过滤器

python - 如何读取 Pandas 数据框中的 JSON 对象

java - 在 RestController 中绑定(bind) JSON 请求时获取 java.lang.NoSuchMethodException : javax.validation.Valid.value() 错误

java - 如何让 Java GUI 面板组件 hibernate ?

javascript - 基于索引数组过滤数组

android - 如何使用 Imageview 解析 json 来显示图像

java - 在 Groovy 类中实现 Java 接口(interface)

java - 单击按钮时更新 jLabel