英文:
Caused by: java.lang.StackOverflowError: stack size 1040KB at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:649)
问题
以下是您要翻译的代码部分:
我正在尝试从JSON对象动态构建查询,使用`StringBuilder`和`Iterator`。
**方法:**
```java
public static void main(String[] args) {
// JSONArray jsonArray = json数组
// for(int i=0;i<jsonArray.length();i++){
// JSONObject jsonObject=jsonArray.get(i);
// 用于示例的JSON
JSONObject jsonObject = new JSONObject(
"{\"SchemaId\":\"186f134a-e65c-4e2d-92c6-04afe6951dd6\",\"SchemaName\":\"CIR\",\"Schema\":\"{}\",\"Parent\":null,\"Application\":\"CA\",\"ModifiedDateTime\":\"2020-08-31T04:21:46.403\",\"Version\":\"3\",\"PageType\":\"Line\",\"favorite\":false,\"CategoryType\":\"bits\",\"IsDeleted\":true}"
);
createPreparedStmtQuery(jsonObject, "mytable");
// }
}
public static String createPreparedStmtQuery(JSONObject record, String tableName) throws JSONException {
record = modifyRecord(record);
Iterator<String> keys = record.keys();
StringBuilder builder = new StringBuilder();
builder.append("insert or replace into ");
builder.append(tableName);
builder.append(" (");
while (keys.hasNext()) {
builder.append(keys.next()).append(",");
}
builder.setLength(builder.length() - 1);
builder.append(") values(");
keys = record.keys();
JSArray valuesArray = new JSArray();
while (keys.hasNext()) {
String key = keys.next();
if (record.get(key) != null && record.get(key) instanceof String) {
}
builder.append("?").append(",");
valuesArray.put(record.get(key));
}
builder.deleteCharAt(builder.length() - 1);
builder.append(");");
JSONObject ret = new JSONObject();
ret.put("query", builder.toString());
ret.put("values", valuesArray);
return ret.toString();
}
private static JSONObject modifyRecord(JSONObject record) throws JSONException {
if (record.has("ValidFrom")) {
record.remove("ValidFrom");
}
if (record.has("ValidTo")) {
record.remove("ValidTo");
}
Iterator<String> keys = record.keys();
while (keys.hasNext()) {
String key = keys.next();
if (record.get(key) instanceof JSONObject || record.get(key) instanceof List) {
record.put(key, ((JSObject) record.get(key)).toString());
}
}
return record;
}
以及类似这样的错误:
Caused by: java.lang.StackOverflowError: stack size 1040KB at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:649)
上述方法中是否存在问题?如何解决它?谢谢。
<details>
<summary>英文:</summary>
I am trying to build query dynamically from json object using `StringBuilder` and `Iterator`.
**method:**
public static void main(String[] args) {
//JSONArray jsonArray = array of json
//for(int i=0;i<jsonArray.length();i++){
//JSONObject jsonObject=jsonArray.get(i);
//for sample json
JSONObject jsonObject=new JSONObject("{\"SchemaId\":\"186f134a-e65c-4e2d-92c6-04afe6951dd6\",\"SchemaName\":\"CIR\",\"Schema\":\"{}\",\"Parent\":null,\"Application\":\"CA\",\"ModifiedDateTime\":\"2020-08-31T04:21:46.403\",\"Version\":\"3\",\"PageType\":\"Line\",\"favorite\":false,\"CategoryType\":\"bits\",\"IsDeleted\":true}");
createPreparedStmtQuery(jsonObject,"mytable");
//}
}
public static String createPreparedStmtQuery(JSONObject record,String tableName) throws JSONException {
record = modifyRecord(record);
Iterator<String> keys = record.keys();
StringBuilder builder = new StringBuilder();
builder.append("insert or replace into ");
builder.append(tableName);
builder.append(" (");
while(keys.hasNext()){
builder.append(keys.next()).append(",");
}
builder.setLength(builder.length() - 1);
builder.append(") values(");
keys = record.keys();
JSArray valuesArray = new JSArray();
while(keys.hasNext()){
String key = keys.next();
if( record.get(key) != null && record.get(key) instanceof String) {
}
builder.append("?").append(",");
valuesArray.put(record.get(key));
}
builder.deleteCharAt(builder.length() - 1);
builder.append(");");
JSONObject ret = new JSONObject();
ret.put("query",builder.toString());
ret.put("values",valuesArray);
return ret.toString();
}
private static JSONObject modifyRecord(JSONObject record) throws JSONException {
if (record.has("ValidFrom")) {
record.remove("ValidFrom");
}
if (record.has("ValidTo")) {
record.remove("ValidTo");
}
Iterator<String> keys = record.keys();
while(keys.hasNext()){
String key = keys.next();
if (record.get(key) instanceof JSONObject || record.get(key) instanceof List) {
record.put(key,((JSObject)record.get(key)).toString());
}
}
return record;
}
**And an Error receiving like this:**
Caused by: java.lang.StackOverflowError: stack size 1040KB at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:649)
Is there any issue in the above methods?How to solve it?
Thanks.
</details>
# 答案1
**得分**: 1
你可以尝试增加你的JVM堆栈大小。在你的情况下,大小似乎是1040KB。你可以将它设置为另一个值(例如48 MB),如下所示:
```bash
java -Xss48m
更多详细信息和示例请参阅https://www.baeldung.com/jvm-configure-stack-sizes或官方参考https://www.oracle.com/java/technologies/javase/vmoptions-jsp.html。
英文:
You could try to increase the stack size of your JVM. In your case, the size seems to be 1040KB. You can set it to another value (e.g. 48 MB) like this:
java -Xss48m
For more details and examples see https://www.baeldung.com/jvm-configure-stack-sizes or the official reference https://www.oracle.com/java/technologies/javase/vmoptions-jsp.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论