我怎么能在这种情况下

我怎么能在这种情况下

问题描述:

我有一个键值对的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

选项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