在这种情况下如何更新JSON

时间:2015-03-03 11:09:16

标签: java json

我有一个JSON对象,其键值对为{"Name":"KEY1"}

我有另一个JSON为

{
    "ServiceTax": 133.24263,
    "VAT": 23
}

如何使用{“Name”:“KEY1”}添加早期的JSON,这两个值也是ServiceTax和VAT

以便JSON看起来像

{
    "NAME" : "KEY1"
    "ServiceTax": 133.24263,
    "VAT": 23
}

这里的问题是名称ServiceTax和VAT不是固定的,可以是任何东西

这是我的程序

import java.text.DecimalFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Test {

    public static void main(String args[]) throws JSONException {

        String json = "{\r\n" + 
                "    \"ServiceTax\": 133.24263,\r\n" + 
                "    \"VAT\": 23\r\n" + 
                "}";

        Map < String, LinkedList < JSONObject >> vendorOrdersMap = new LinkedHashMap < String, LinkedList < JSONObject >> ();

        JSONObject json_obj_for_key1 = new JSONObject();  json_obj_for_key1.put("Name", "KEY1");
        JSONObject json_obj_for_key2 = new JSONObject();  json_obj_for_key2.put("Name", "KEY2"); 

        LinkedList list1 = new LinkedList(); list1.add(json_obj_for_key1);

        LinkedList list2 = new LinkedList(); list2.add(json_obj_for_key2);

        vendorOrdersMap.put("ONE", list1);
        vendorOrdersMap.put("TWO", list2);

        for (Map.Entry < String, LinkedList < JSONObject >> entry: vendorOrdersMap.entrySet())
        {
            String key = entry.getKey();
            LinkedList < JSONObject > json_list = entry.getValue();

            for (JSONObject json_data: json_list)
            {
                // 
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

选项1:

//set up your keys/key object(s) however you prefer
JSONObject sourceObj = new JSONObject();
sourceObj.put("NAME", "KEY<n>");

JSONObject destObj = new JSONObject(json);  //parse the json
destObj.put("NAME", sourceObj.get("NAME"));  //merge in the field from the source object

选项2:

//function to merge the values in two JSON objects into a single resulting
//object; this will return a new object instance that can be manipulated 
//independently of the source object(s), or null if both source objects are 
//null.
//
//Note that fields in 'obj2' take priority if a collision occurs.
public static JSONObject mergeJson(JSONObject obj1, JSONObject obj2) throws JSONException {
    //cover the easy cases
    if (obj1 == null && obj2 == null) {
        return null;
    }
    if (obj1 == null) {
        return new JSONObject(obj2.toString());
    }
    if (obj2 == null) {
        return new JSONObject(obj1.toString());
    }

    //merge the two inputs
    JSONObject result = new JSONObject();

    Iterator<String> keys = obj1.keys();
    while (keys.hasNext()) {
        String key = keys.next();
        result.put(key, obj1.get(key));
    }

    keys = obj2.keys();
    while (keys.hasNext()) {
        String key = keys.next();
        result.put(key, obj2.get(key));
    }

    return result;
}

//then elsewhere in your code...

//set up your keys/key object(s) however you prefer
JSONObject obj1 = new JSONObject();
obj1.put("NAME", "KEY<n>");

JSONObject obj2 = new JSONObject(json);  //parse the json
JSONObject destObj = mergeJson(obj1, obj2); //merge the two objects