2015-03-03 150 views
0

我有一個鍵值對的JSON對象爲{"Name":"KEY1"}我怎麼能在這種情況下

我有另一個JSON作爲

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

如何添加較早的JSON與{」更新JSON名稱 「:」 KEY1" }這兩個值也ServiceTax和增值稅

使得JSON看起來像

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

這裏的問題是,該名ServiceTax和增值稅是不固定的,並且可以是任何東西

這是我的計劃

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) 
      { 
       // 
      } 
     } 
    } 
} 
+0

帶有變量鍵的JSON是一個String還是一個JSONObject? – Soana 2015-03-03 13:12:14

+0

你是否結婚使用'org.json。*'類?如果沒有,您可能會發現[simple-json](https://code.google.com/p/json-simple/)或[gson](https://code.google.com/p/google-gson/ )更易於使用。 – aroth 2015-03-03 14:17:22

回答

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