2016-07-13 83 views
-6

我有這個字符串字符串地圖<字符串,對象>

"{id={date=1467991309000, time=1467991309000, timestamp=1467991309, timeSecond=1467991309, inc=-360249353, machine=-705844029, new=false}, id_lista={date=1467991309000, time=1467991309000, timestamp=1467991309, timeSecond=1467991309, inc=-360249354, machine=-705844029, new=false}, id_documento=1297183, estado=1, fecha_ing=1467991309026, fecha_mod=1468010645484, identificador_r=null, titulo=null, pais=null, sector=null, url=null, dato1=null, dato2=null}" 

我怎樣才能在Java解析得到這樣的事情Map<String,Object>

id:{} 
    id_lista:{} 
    id_documento:123 
    estado:1 
    fecha_ing:1467991309026 
    etc.. 

更新:

  • 最後,我將其轉換爲JSONArray以獲得值ES。
+1

你是什麼意思的映射?你向我們展示了一個字符串和另一個字符串,你想將一個字符串轉換爲另一個字符串? –

+0

不,我需要一個來自字符串 – Bosses

+2

的Map 我認爲你的意思是'parsing'而不是'mapping'。它看起來像你試圖解析的JSON格式。 – vlatkozelka

回答

1

你真的是指java.lang.Object還是你的意思是你自己製作的一類?

您可以進入Java世界如果你有正確定義的您對下列(Google Gson)類:

BossesClass hisClass = new Gson().fromJson(bossesString, BossesClass.class); 

你在地圖中的鍵值(字符串)使用什麼是你的決定

0

它看起來好像你有一個幾乎JSON格式的字符串。 取決於你想使用你的地圖,也許你想使用org.json.JSONObject而不是? (當你在你的示例字符串中嵌套信息時,這是非常好的。)

要從字符串中獲取JSONObject,首先必須用冒號代替所有等號。

String jsonString = "your string here".replace("=",":"); 

然後你可以創建一個JSONObject。

JSONObject jsonObj = new JSONObject(jsonString); 

如果你無論如何想有地圖,有答案herehere有關從JSONObject的獲取到地圖。

public void parse(String json) { 
     JsonFactory factory = new JsonFactory(); 

     ObjectMapper mapper = new ObjectMapper(factory); 
     JsonNode rootNode = mapper.readTree(json); 

     Iterator<Map.Entry<String,JsonNode>> fieldsIterator = rootNode.fields(); 
     while (fieldsIterator.hasNext()) { 

      Map.Entry<String,JsonNode> field = fieldsIterator.next(); 
      System.out.println("Key: " + field.getKey() + "\tValue:" + field.getValue()); 
     } 
} 
相關問題