簡體   English   中英

僅在首次出現時使用特殊符號在Java中拆分字符串?

[英]Split string in Java with special symbol in first occurence only?

我想在HTML頁面中顯示Java列表數據。 因此,我認為使用JSON更好。 我將列表轉換為json字符串對象。 我可以使用Jquery解析並在列表中顯示信息。 我將列表轉換為json對象。 轉換如下:

{"list":[{"one":"11","two":"21","three":31},{"one":"12","two":"22","three":32},{"one":"13","two":"23","three":33}]}

但是我知道jQuery語法幾乎不匹配。
jQuery僅在我通過谷歌搜索才知道處理以下內容:

'[{"one":"11","two":"21","three":31},{"one":"12","two":"22","three":32},{"one":"13","two":"23","three":33}]'

我只想丟棄:冒號)的子字符串,最后一個char {也丟棄,那么我可以傳遞此json對象,而jQuery處理得很好。 我嘗試了兩個字符串,只有第二個起作用。 如何使用正則表達式丟棄第一次出現的:子字符串?

jQuery可以同時處理兩者。 (它們都是正確的JSON格式。)

var obj = $.parseJSON(data);

// for the first type
var arr = obj.list;

// for the second type obj is already an array.

檢查演示。

以下代碼將為您提供所需的字符串:

String str = "{"list":[{"one":"11","two":"21","three":31},{"one":"12","two":"22","three":32},{"one":"13","two":"23","three":33}]}" // Escape the Quotes to include in str

str = str.substring(str.indexOf(':')+1,str.length()-1);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM