0

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?

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Mr.Chowdary
  • 3,389
  • 9
  • 42
  • 66
  • Why you only use jquery? Javascript also parse your json too.http://www.json.org/js.html – swemon Sep 24 '12 at 10:34
  • @swemon Check this question http://stackoverflow.com/questions/10362277/jquery-parsejson-vs-json-parse – xdazz Sep 24 '12 at 10:38

2 Answers2

3

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.

xdazz
  • 158,678
  • 38
  • 247
  • 274
  • Hi xdazz, I tried but it is not working with the first example code.. only working with second example code.. – Mr.Chowdary Sep 24 '12 at 10:33
  • @Mr.Chowdary Check the demo I added. – xdazz Sep 24 '12 at 10:35
  • I checked it.. Working well. I just not aware of adding the name as $.parseJSON(string1).list);.. Finally i got to know.. Thanks a lot xdazz... For your time with Me... – Mr.Chowdary Sep 24 '12 at 10:44
3

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);
Azodious
  • 13,752
  • 1
  • 36
  • 71