• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to convert string from Json.stringify back to Json or iterable object Java?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
I have following string which I recieved from Jquery post. Original object was modified to string with Json.stringify.

a) I tried with following but I only return first json object.
JSONObject obj = new JSONObject(jsonData);
System.out.println("id: " + obj.getString("id"));
System.out.println("price: " + obj.getString("price"))

b.) I tried regex with filter="\\{,\\}" for String.split(filter) but it still doesn't work.

Here is string:
{"id":"Coat summer","price":"$58.99"},{"id":"Coat summer","price":"$58.99"},{"id":"Coat winter","price":"$128.99"}

Thank you.
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That string is not valid JSON.

If you want multiple objects, it should be enclosed in angle brackets:
You can then parse it as a JSON array, instead of a JSON object.
 
Erde Erik
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. Solved.
 
Erde Erik
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for help. By the way how can I solve in regex way. How do we split that string by },{ then make new array of Strings?

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shouldn't use regular expressions to parse JSON, XML, etc.
 
reply
    Bookmark Topic Watch Topic
  • New Topic