• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

trying to work with JSON-lib to create JSON string

 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

Hopefully there's someone here who has worked with JSON-lib before? I'm trying to create a JSON string using a 2D String array. Each String must have the format: "field_name":"field_value"

I want the output to be like:
["LASTNAME":"MacDonald", "FIRSTNAME":"Terry", "EMAIL_ADDRESS":"tmacdonald@someplace.com"],
["LASTNAME":"Steele","FIRSTNAME":"Paul","EMAIL_ADDRESS":"psteele@someplace.com"]

I've tried the following, which unfortunately doesn't output the strings properly:
String[][] array = new String[][];
array[0][0] = "\"LASTNAME\":\"MacDonald\"";
array[0][1] = "\"FIRSTNAME\":\"Terry\"";
array[0][2] = "\"EMAIL_ADDRESS\":\"tmacdonald@someplace.com\"";
...
JSONObject jsonArray = JSONArray.fromObject(array);
System.out.println( jsonObject );

This results in:
["\"LASTNAME\":\"MacDonald\"", "\"FIRSTNAME\":\"Terry\"", "\"EMAIL_ADDRESS\":\"tmacdonald@someplace.com\""],
["\"LASTNAME\":\"Steele\"","\"FIRSTNAME\":\"Paul\"","\"EMAIL_ADDRESS\":\"psteele@someplace.com\""]

Escaping the quote symbol obviously isn't the answer here. What do I need to do to get the format I'm looking for? Please advise.

Alan
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You can use GSON library. This is pretty good for formatting.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic