Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I stringify array type properties of a Java object using package org.json
    primarykey
    data
    text
    <p>I am trying to stringify a typical Java object using the <a href="http://www.json.org/javadoc/org/json/JSONObject.html#toString%28%29" rel="nofollow">toString()</a> method from org.json. However, Collections or Array type members in my beans are stringified in the form of byte arrays. I have set out a sample code to illustrate the problem.</p> <p>Here is what I do expect:</p> <pre><code>{ "nonWorkingDays":[ "Saturday", "Sunday" ], "workingDays":[ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" ], "days":[ 1, 2, 3, 4, 5, 6, 7 ] } </code></pre> <p>And here is what I am getting instead:</p> <pre><code>{ "nonWorkingDays": [ { "bytes": [ {}, {}, ... ], "empty": false }, { "bytes": [ {}, {}, ... ], "empty": false } ], "workingDays": [ { "bytes": [ {}, {}, ... ], "empty": false }, { "bytes": [ {}, {}, ... ], "empty": false }, ... ], "days": [ {}, {}, ... ] } </code></pre> <p>Finally, here is my code:</p> <pre><code>/* Week.java */ import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Week { private List&lt;String&gt; workingDays = new ArrayList&lt;String&gt;(); private String[] nonWorkingDays; int days[]; public Week() { String[] workingDaysArray = new String[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" }; workingDays.addAll(Arrays.asList(workingDaysArray)); nonWorkingDays = new String[] { "Saturday", "Sunday" }; days = new int[] { 1, 2, 3, 4, 5, 6, 7 }; } public List&lt;String&gt; getWorkingDays() { return workingDays; } public String[] getNonWorkingDays() { return nonWorkingDays; } public int[] getDays() { return days; } @Override public String toString() { return "Week [workingDays=" + workingDays + ", nonWorkingDays=" + Arrays.toString(nonWorkingDays) + ", days=" + Arrays.toString(days) + "]"; } } /* WeekTest.java */ import org.json.JSONException; import org.json.JSONObject; public class WeekTest { public static void main(String[] args) throws JSONException { Week myWeek = new Week(); JSONObject myWeekJSON = new JSONObject(myWeek); String myWeekJSONString = myWeekJSON.toString(2); System.out.println(myWeekJSONString); // Formatted JSON string System.out.println(myWeek); // Output from Week's toString() } } </code></pre> <p>Btw, the Week's toString() method output's the following:</p> <pre><code>Week [workingDays=[Monday, Tuesday, Wednesday, Thursday, Friday], nonWorkingDays=[Saturday, Sunday], days=[1, 2, 3, 4, 5, 6, 7]] </code></pre> <p>Edit:</p> <p>The problem seems to be with <a href="http://mvnrepository.com/artifact/org.json/json/20090211" rel="nofollow">this maven artifact</a>. After updating to the source from <a href="https://github.com/douglascrockford/JSON-java" rel="nofollow">this github repo</a> I got everything fixed. Please see comment below.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload