Currently forming the JSON using the below string in java , will be used in multithreading environment and values will be dynamic after passing the values this JSON will be posted to the 3 rd party web service
"{"
+ " \"test\": {"
+ " \"testCont\": {"
+ " \"size\": {"
+ " \"size\": " + size +","
+ " \"number\": " + number
+ " }"
+ " },"
+ " \"testDate\": {"
+ " \"testAccount\": {"
+ " \"name\": ["
+ testMethod.getName()
+ " ],"
+ " \"school\": {"
+ " \"schoolIdentity\": ["
+ testMethod.schoolIdentity()
+ " ]"
+ " }"
+ " }"
+ " }"
+ " }"
+ "}"
I am aware that using String concatenation will create unnecessary objects in heap
Can some one please tell me the most efficient way to create which will not create additional objects in heap and help to gain performance ?
IF you are converting from Java POJO you can use
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
This will give you the JASON String
Also you can use
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
Refer this https://www.geeksforgeeks.org/convert-java-object-to-json-string-using-jackson-api/
Related
i developed android app and i used HTML code to Print the result in formatted form.
the code will be like this :
String adress = editText1.getText().toString();
String phone = editText2.getText().toString();
String licenseNo = editText3.getText().toString();
"\n" +
" <div class=\"down\">\n" +
" <div class=\"EASY\">\n" +
" <img src=\"" + image + "\" alt=\"QR\" >\n" +
" </div>\n" +
" <table class=\" info_table\">\n" +
" <tbody>\n" +
" <tr>\n" +
" <td class=\"info\">" + phone + "</td>\n" +
" <td class=\"info\"> " + licenseNo + "</td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td class=\"info_A\" colspan=\"2\"> " + adress + "</td>\n" +
" </tr>\n" +
" </tbody>\n" +
" </table>\n" +
" </div>";
as you see it should contain \n and + because its inside a string object contained variable values from user input.
i faced some difficulty when i need to Edit this Piece of code cose i will test it inside the application.
my question is what is the best way to get this code to edit and preview it outside android studio and reinsert it again.
i used find and replace but it make some problems.
hope i explained enough
You can create a XML file in the resources folder, read it and then use format in order to change the values there.
I have below JSON returned by API call.
String jsonString="{\r\n" +
" \"count\": 100,\r\n" +
" \"limit\": 100,\r\n" +
" \"totalResults\": 225,\r\n" +
" \"hasMore\": true,\r\n" +
" \"items\": [\r\n" +
" {\r\n" +
" \"id\": \"55D40522-8672-48B0-B225-FD3CE6686AD6\",\r\n" +
" \"name\": \"AcmeHCMExtended\",\r\n" +
" \"version\": \"20.01.01.03\",\r\n" +
" \"status\": \"DRAFT\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"id\": \"2DB4C83B-0CF9-4A8E-AC41-A29B30324121\",\r\n" +
" \"name\": \"AFinancialBot\",\r\n" +
" \"version\": \"1.0\",\r\n" +
" \"status\": \"DRAFT\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"id\": \"7EA85B81-3CA1-4BE0-B095-217E7C93DCEF\",\r\n" +
" \"name\": \"AIAMFG\",\r\n" +
" \"version\": \"1.0\",\r\n" +
" \"status\": \"DRAFT\"\r\n" +
" }"
I want to read name one by one under the itmes. I am using below code.
JsonObject obj=Json.parse(jsonString).asObject();
JsonArray items=obj.get("items").asArray();
System.out.println(items); //it prints everything under items.
`System.out.println(items.name); ` //It gives error
I am not able to iterate item array. it does not show length property of array.
Can you guide me how can I access element one by one.
It looks like you are using the javax.json.JsonArray APIs or something similar. If so:
JsonArray items = obj.get("items").asArray();
for (int i = 0; i < items.size(); i++) {
System.out.println(items.getJsonObject(i));
}
Note that JsonArray implements java.util.List and you are getting the size using the standard List API method.
The same code would work with com.google.gson.JsonArray ... though this class does NOT implement List. Alternatively, you could make use of the fact that com.google.gson.JsonArray implements Iterable and write a "for each" loop:
for (JsonElement e: items) {
System.out.println(e);
}
The equivalent code for org.json.JSONArray uses length() instead of size().
In all cases, you should be able to answer your own question by searching for and reading the javadocs for the JSON library that you are using.
UPDATE - Apparently you are using the com.eclipsesource.minimal-json. According to this javadoc, it has a size() method and implements Iterable.
I'm making an XQuery on a java code that generates a file extracting data from two different xml files from eXistDB, but when I generate the output xml it adds xmlns="" tag on every child element.
This is the output xml that i'm getting:
<?xml version='1.0' encoding='UTF-8'?>
<produc>
<cod_prod xmlns="">1011</cod_prod>
<denominación xmlns="">Micro Intel Core i5-2320</denominación>
<precio xmlns="">120</precio>
<nombre_zona xmlns="">Madrid-CENTRO</nombre_zona>
<director xmlns="">Pedro Martín</director>
<stock xmlns="">-2</stock>
</produc>
<produc>
<cod_prod xmlns="">1019</cod_prod>
<denominación xmlns="">Memoria DDR3 G.Skill 2GB</denominación>
<precio xmlns="">10</precio>
<nombre_zona xmlns="">Madrid-CENTRO</nombre_zona>
<director xmlns="">Pedro Martín</director>
<stock xmlns="">2</stock>
</produc>
<produc>
<cod_prod xmlns="">1020</cod_prod>
<denominación xmlns="">Memoria DDR3 G.Skill 4GB</denominación>
<precio xmlns="">30</precio>
<nombre_zona xmlns="">Madrid-CENTRO</nombre_zona>
<director xmlns="">Pedro Martín</director>
<stock xmlns="">20</stock>
</produc>
This is the XQuery used to generate the output:
consulta = conn.prepareExpression("for $prod in //produc[cod_zona="+zona+"] " +
"let $nombre_zona:=//zona[cod_zona = $prod/cod_zona]/nombre " +
"let $director:=//zona[cod_zona = $prod/cod_zona]/director " +
"return" +
" <produc>" +
" <cod_prod>{data($prod/cod_prod)}</cod_prod> " +
" <denominación>{data($prod/denominacion)}</denominación> " +
" <precio>{data($prod/precio)}</precio> " +
" <nombre_zona>{data($nombre_zona)}</nombre_zona> " +
" <director>{data($director)}</director> " +
" <stock>{data($prod/stock_actual)-data($prod/stock_minimo)}</stock> " +
" </produc>"
);
I am using HTTP Client to send different kinds of requests (GET, PUT, POST, DELETE) where I am sending the JSON as a data to post with different requests.
I have a JSON data like:
{ "createdByID": "100000", "createdByName": "Admin", "modifiedByID": "100000", "modifiedByName": "Admin" }
Now, to store this JSON into a string, I have to add double quotes wherever necessary so that this can be stored as
String jsonData = "{" + "\"" + "createdByID" + "\"" + ":" + "\"" + "100000" + "\"" + "," + "\"" + "createdByName" + "\"" + ":" + "\"" + "Admin" + "\"" + "," + "\"" + "modifiedByID" + "\"" + ":" + "\"" + "100000" + "\"" + "," + "\"" + "modifiedByName" + "\"" + ":" + "\"" + "Admin" + "\"" + "}"
Does anyone use any tool/utility to convert the JSON data such that it can be stored in a string object?
Please share if anyone has already done this
Hey if you store the json as a sting in your code then only you need to add double codes.
Try to read json from a text file , then you don't need to add double quotes and other stuffs.
More over java does not consider double quotes after compiling your code.
I am creating meeting request with java code and sending it to full as well as lite version of outlook as shown below:
final SimpleDateFormat iCalendarDateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmm'00'");
final long uid =System.currentTimeMillis();
iCalendarDateFormat.setTimeZone(TimeZone.getTimeZone(MRBSConstants.TIMEZONE));
final String calendarContent = "BEGIN:VCALENDAR\n"
+ "METHOD:REQUEST\n"
+ "PRODID: BCP - Meeting\n"
+ "VERSION:2.0\n"
+ "BEGIN:VEVENT\n"
+ "DTSTAMP:"+ iCalendarDateFormat.format(meetingEndTime) + "\n"
+ "DTSTART:" + iCalendarDateFormat.format(meetingStartTime) + "\n"
+ "DTEND:"+ iCalendarDateFormat.format(meetingEndTime) + "\n"
+ "SUMMARY:test request\n"
+ "UID:" + uid + "\n"
+ "ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:MAILTO:" + Arrays.toString(recipetList) + "\n"
+ "ORGANIZER:MAILTO:" + from+ "\n"
+ "LOCATION:" +loc + "\n"
+ "DESCRIPTION:" + body+ "\n"
+ "SEQUENCE:0\n" + "PRIORITY:5\n" + "CLASS:PUBLIC\n" + "STATUS:CONFIRMED\n" + "TRANSP:OPAQUE\n" + "BEGIN:VALARM\n"
+ "ACTION:DISPLAY\n" + "DESCRIPTION:REMINDER\n" + "TRIGGER;RELATED=START:-PT00H15M00S\n" + "END:VALARM\n" + "END:VEVENT\n" + "END:VCALENDAR";
And it is working fine with lite version of outlook means it giving correct time in lite version but in full version of outlook it showing different timings.?why
Here MRBSConstants.TIMEZONE value is GMT-5:30.
I have also tried adding VTIMEZONE COMPONENT but in that case outlook cant recognize the ics file as correct.
Do we have any general ics object which can work on both version?