将JSON值放入Hashmap [英] Putting JSON values into Hashmap

查看:251
本文介绍了将JSON值放入Hashmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有如下所示的JSON值

I have JSON value like below,

   { "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的下一个数组作为下一个键和值(整个数组应成为一个键和值).有人请帮忙.

now I want to add next array that is direct_report to put as next key and value(entire array shoud be come one key and value). Someone please help out.

推荐答案

哈希图是键/值存储,其中键是唯一的.您可以将JSON转换为字符串,然后将其作为值存储到哈希图中.例如以下内容:

Hashmap is a key/value storage, where keys are unique. You can convert your JSON to string and then store it as a value to the hashmap. For example something like below:

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

您还可以使用'

  • 手动创建JSON对象
  • 将现有的JSONObject转换为字符串表示形式
  • 将JSON字符串转换为JSONObject
  • 您还可以采用以下解决方案:

    You can also have the following solution:

    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.您可以从此处.

    Make sure that you download the org.json jar file and put it in your classpath to be able to use the JSONObject. You can download the jar from here.

    为了将这些值中的每一个作为单个键/值输入项放入映射中.您自己提到了它,它应该可以正常工作.请参见以下方法:

    In order to put each of those values into map as single key/value entry. You have mentioned it yourself, it should work without any problem. See below methods:

    方法1 Java中的一切都是对象,字符串继承对象,字符串[]继承对象.您可以采用以下解决方案:

    Method 1 Everything in Java is Object, String inherits Object, String[] inherits object. You can have the following solution:

    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 要将字段存储为键/值,并且如果您有能力将数组转换为String(表示所有以逗号分隔的数组元素,请使用此方法).

    Method 2 To store the fields as key/value and if you can afford converting the array to String (which represents all array elements comma separated then use this method).

    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 以拥有散列图来存储字符串键和数组值为代价.您还必须将其他元素作为数组放置.

    Method 3 In the expense of having Hash Map to store String key and Array value. You have to put other elements as array too.

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

    这篇关于将JSON值放入Hashmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆