简体   繁体   中英

Split string in Java with special symbol in first occurence only?

I want to display the java list data in the html page. So i decided it is better to work with JSON. I converted the list into json string object. Using Jquery i can parse and display the info in the list. I converted the list into json object. It is converted as follows:

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

But I came to know that the syntax is little mismatched to parse by jQuery.
jQuery is handling the following one only as I came to know by googling:

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

I just want to discard the substring occurrence of : (colon) and last char { also discards, Then I can pass this json object and jQuery is handling well. I tried with the both strings, only second one works. How can I discard the substring with the first occurrence of : with regex?

jQuery could handle both. (They are both right JSON format.)

var obj = $.parseJSON(data);

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

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

Check the demo.

Following code will give you required string:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM