2017-10-04 74 views
5

我有一個非常複雜的json結構。它包含很多array elements,那些數組元素包含其他數組元素等等。 請看下面的json樹形結構。如何檢索和更新json數組元素而不遍歷整個json

的Json樹結構-1:

enter image description here

的Json樹結構-2:

enter image description here

如黃色上面所強調的,我要更新的值「rdKey」字段。 我寫了下面的代碼,它是完美的罰款工作:

String json = "escaped string (as it's a big string, I can't put it here)"; 
JSONObject jsonObj = new JSONObject(json); 

    if (jsonObj.has("responseMap")) { 
     JSONObject responseMap = jsonObj.getJSONObject("responseMap"); 
     if (responseMap.has("ValueJson")) { 
      JSONObject valueJson = responseMap.getJSONObject("ValueJson"); 
      if (valueJson.has("ticketBean_CM")) { 
       JSONObject ticketBean_CM = valueJson.getJSONObject("ticketBean_CM"); 
       if (ticketBean_CM.has("addByGamma")) { 
        String addByGamma = ticketBean_CM.getString("addByGamma"); 
        System.out.println(addByGamma); 

        if (addByGamma.equals("VCE")) { 
         if (responseMap.has("ScreenJson")) { 
          JSONObject screenJson = responseMap.getJSONObject("ScreenJson"); 
          if (screenJson.has("sections")) { 
           JSONArray sectionArray1 = screenJson.getJSONArray("sections"); 
           if (sectionArray1.length() > 0) { 
            JSONObject section0 = sectionArray1.getJSONObject(0); 
            if (section0.has("sections")) { 
             JSONArray sectionArray2 = section0.getJSONArray("sections"); 
             if (sectionArray2.length() > 3) { 
              JSONObject section6 = sectionArray2.getJSONObject(3); 
              if (section6.has("sections")) { 
               JSONArray sectionArray3 = section6.getJSONArray("sections"); 
               if (sectionArray3.length() > 1) { 
                JSONObject section8 = sectionArray3.getJSONObject(1); 
                if (section8.has("elements")) { 
                 JSONArray elementsArray1 = section8 
                   .getJSONArray("elements"); 
                 if (elementsArray1.length() > 0) { 
                  JSONObject elements1 = elementsArray1.getJSONObject(0); 
                  if (elements1.has("elements")) { 
                   JSONArray elementsArray2 = elements1 
                     .getJSONArray("elements"); 
                   if (elementsArray2.length() > 4) { 
                    JSONObject elements2 = elementsArray2 
                      .getJSONObject(4); 
                    if (elements2.has("rdKey")) { 
                     System.out.println(
                       elements2.getString("rdKey")); 
                     elements2.put("rdKey", 
                       "CircuitID(FullPartial)"); 
                     System.out.println(
                       elements2.getString("rdKey")); 
                     System.out.println(jsonObj.toString()); 
                    } 
                   } 
                  } 
                 } 
                } 
               } 
              } 
             } 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 

我希望你們幫我,如果有這方面的任何更好的解決方案。我可以在沒有遍歷整個json對象的情況下執行操作(直到找到相關字段)?如果json樹結構發生變化,此解決方案將不起作用,它需要是靜態的,作爲此代碼的成功場景。

請建議更好的解決方案。

+0

因爲你的編碼風格幾乎相同,所以你可以使用'for loop'作爲[「responseMap」,「ValueJson」,「ticketBean_CM」,...,4] –

+0

我沒有通過它,請你幫忙。 – Punit

+0

看起來你的結構中有很多條件(比如數組中的位置和一些列名)。它是一個特定的約束,就像你以後不需要類似的東西,或者你需要經常通過這些數組?對於後者,我建議創建表示您的對象的Java bean以便於訪問。否則,您可以創建自己的小API以避免重複「如果元素等等」。 – Asoub

回答

1

如果您可以靈活使用什麼庫,那麼JsonPath可能對您有用。

您可以更新所有「元素」與「rdKey」使用下面的代碼:

JsonPath.parse(json).set("$..elements[?(@.rdKey)].rdKey", "CircuitID(FullPartial)").json() 
+0

我需要只更新「rdKey」的單個實例並非全部。 – Punit

+0

如果你不需要在對象中轉換json,Punit的建議是最短的。否則,您可以使用[Jackson]的ObjectMapper [1] – java4fun

+0

對不起,以前的評論是不完整的:如果您不需要在對象中轉換json,則Punit的建議是最短的。可能你必須添加一些過濾器來改變你只能使用這些rdKey。否則你可以用[Jackson(Example)](http://tutorials.jenkov.com/java-json/jackson-objectmapper.html)中的ObjectMapper嘗試,但是你必須用對象構建完整的結構,讀取它,使在正確的地方更改,並在最後重寫它... – java4fun

1

如果你想逃離JSON的穿越,那麼你可以使用JSONPointer,可以在同一org.json庫。 例如: -

String query = <json_pointer_query to element array> 
JSONPointer pointer = new JSONPointer(query); 
JSONObject elementsArrayJSON = (JSONObject) pointer.queryFrom(jsonObj); 
elementsArrayJSON.put("rdKey","CircuitID(FullPartial)"); 

JSON指針查詢語言可以被稱爲:

https://tools.ietf.org/html/rfc6901

注: JSON指針是非常基本的,它不支持通配符。所以你需要確定元素名稱,否則會拋出異常。

+0

看來你已經修改了json字符串,以便它被JSONPointer接受,但我不能修改/修改輸入json,這是我從另一個進程獲得的。在你的String查詢中。 – Punit

+0

這只是查詢字符串的一個例子,您可以相應地修改它 –