将嵌套形式的数据从 extjs 形式添加到 json 对象
Add data in nested form to a json object from extjs form
我有一个用 extjs 编码的 table,在面板内,用户在 table 的两列中输入信息。我希望将此数据添加到主 json 对象内的 json 对象中。
例如: Table 'stats' 有 2 个字段 x 和 y。
我希望我的 json 看起来像 :
{
....
stats: [
{
x : ..,
y : ..
},
{
x : ..,
y : ..
}
// this goes on depending upon the number of rows of entries in the table
]
}
请指教我如何实现这个。如果我将一个以 String 为键、以 List<String>
为值的 HashMap 然后写入
new JSONObject(map);
这行得通吗?
你可以选择实现类似下面的东西,它给你这个 JSON 字符串,你可以在 js 文件中解析它以呈现 extjs 形式:
{"stats":[{"y":"y0","x":"x0"},{"y":"y1","x":"x1"},{"y":"y2","x":"x2"},{"y":"y3","x":"x3"},{"y":"y4","x":"x4"}]}
代码:
JSONObject obj = new JSONObject();
JSONArray array = new JSONArray();
for (int i = 0; i < 5; i++) {
JSONObject element = new JSONObject();
element.put("x", "x" + i);
element.put("y", "y" + i);
array.put(element);
}
obj.put("stats", array);
System.out.println(obj);
我有一个用 extjs 编码的 table,在面板内,用户在 table 的两列中输入信息。我希望将此数据添加到主 json 对象内的 json 对象中。
例如: Table 'stats' 有 2 个字段 x 和 y。 我希望我的 json 看起来像 :
{
....
stats: [
{
x : ..,
y : ..
},
{
x : ..,
y : ..
}
// this goes on depending upon the number of rows of entries in the table
]
}
请指教我如何实现这个。如果我将一个以 String 为键、以 List<String>
为值的 HashMap 然后写入
new JSONObject(map);
这行得通吗?
你可以选择实现类似下面的东西,它给你这个 JSON 字符串,你可以在 js 文件中解析它以呈现 extjs 形式:
{"stats":[{"y":"y0","x":"x0"},{"y":"y1","x":"x1"},{"y":"y2","x":"x2"},{"y":"y3","x":"x3"},{"y":"y4","x":"x4"}]}
代码:
JSONObject obj = new JSONObject();
JSONArray array = new JSONArray();
for (int i = 0; i < 5; i++) {
JSONObject element = new JSONObject();
element.put("x", "x" + i);
element.put("y", "y" + i);
array.put(element);
}
obj.put("stats", array);
System.out.println(obj);