user_id=1
&homework_name=2
&textbook_id=3
§ion_id=4
&qut[0][stem_text]=11
&qut[0][stem_picture]=22
&qut[0][question_type]=1
&qut[0][reference_answers_text]=111
&qut[0][reference_answers_pciture]=222
&qut[0][qpt][0][pack_no]=1
&qut[0][qpt][0][reference_answers]=1111
&qut[0][qpt][1][pack_no]=2
&qut[0][qpt][1][reference_answers]=2222
&qut[1][stem_text]=22
&qut[1][stem_picture]=33
&qut[1][question_type]=2
&qut[1][reference_answers_text]=444
&qut[1][reference_answers_pciture]=222
&qut[1][qpt][0][pack_no]=1
&qut[1][qpt][0][reference_answers]=1111
&qut[1][qpt][1][pack_no]=2
&qut[1][qpt][1][reference_answers]=2222
希望转换成下面这样的json格式:
{
user_id:1,
homework_name:2,
....
qut:[
{
stem_text:11,
stem_picture:22,
....
qpt:[
{
pack_no:1,
reference_answers:111,
....
},{
pack_no:2,
reference_answers:222,
....
},....]
},{
stem_text:11,
stem_picture:22,
....
qpt:[
{
pack_no:1,
reference_answers:111,
....
},{
pack_no:2,
reference_answers:222,
....
},....]
},...]
}
{ "user_id": 1, "homework_name": 2, "textbook_id": 3, "section_id": 4, "qut": [ { "stem_text": 11, "stem_picture": 22, "question_type": 1, "reference_answers_text": 111, "reference_answers_pciture": 222, "qpt": [ { "pack_no": 11, "reference_answers": 111 }, { "pack_no": 2, "reference_answers": 2222 } ] }, { "stem_text": 11, "stem_picture": 22, "question_type": 1, "reference_answers_text": 111, "reference_answers_pciture": 222, "qpt": [ { "pack_no": 11, "reference_answers": 111 }, { "pack_no": 2, "reference_answers": 2222 } ] } ] }
值50分的
真心让你委屈了,怪我没把问题描述好,是用java程序将此字符串转成json格式字符串
@刺风:你个挨千刀的...最简单的办法是 formdata->obj->json ,先写一个form数据反序列化.在用json序列化. obj->json只要firstjson就行了.难点肯定是在formdata->obj
不考虑性能的话就是一个递归反射的事情.
@吴瑞祥: 整点干货,( ̄ ‘i  ̄;)
@刺风: 干活就是"递归反射"要代码的话.找别人吧..
哈哈,要是让我写,我就一点一点凑出来,估计会很痛苦。好像有生成 方法的。
必须痛苦,我之前也尝试写过,先获取1级树然后获取二级树,以此类推 但是觉得这么写太操蛋了而且效率特低。
@刺风: 是的,会贼操蛋。这个问题搞定了把解决代码贴出来一下,我也学一下
@威廉姆斯·苑: 必须滴!
{"user_id":1},{"homework_name":2},{"textbook_id",3},{"section_id":4},{"qut":[{[{"stem_text":11},{"stem_picture":22},{"question_type":1},{"reference_answers_text":111},{"reference_answers_pciture":222}{"qpt":[{[{"pack_no":1},{"reference_answers":1111}]},{[{"pack_no":2},{"reference_answers":2222}]}]}]},{[{"stem_text":22},{"stem_picture":33},{"question_type":2},{"reference_answers_text":444},{"reference_answers_pciture":222},{"qpt":[{[{"pack_no":1},{"reference_answers":1111}]},{[{"pack_no":2},{"reference_answers":2222}]}]}]}]}
这个还好,不算特别长
使用字符串慢慢拼接呗!如果是通用的就手写一个方法
这是url传递过来的?如果是的话,在前一个页面做修改
你说对了,这个是用restful web service 接收post 提交过来的json数据,它自己给转成这这样了,我都无语了
@刺风: 这样的数据不符合常规,一律改成json格式,要不然专门写个解析的方法,影响心情不说,还加了工序,改吧
public static String getjson(String param) {
String one = "questions";
String two = "questions";
JSONArray json = new JSONArray();
JSONArray ja = new JSONArray();
List<String> oneList = new ArrayList<String>();
List<String> twoList = new ArrayList<String>();
List<String> threeList = new ArrayList<String>();
String[] params = param.split("&");
for (String s : params) {
if (s.indexOf(one + "[") == -1) {
oneList.add(s);
} else {
if (s.indexOf(two + "]") == -1) {
twoList.add(s);
} else {
threeList.add(s);
}
}
}
JSONObject oneobj = new JSONObject();
for (String s : oneList) {
s = s.replaceFirst("=", ":");
int len = s.indexOf(":");
String key = s.substring(0, len);
String value = s.substring(len + 1);
// System.out.println(key+":"+value);
oneobj.put(key, value);
}
String onelen = one + "[";
List<Integer> oneNumList = new ArrayList<Integer>();
for (String s : twoList) {
String ss = s.substring(s.indexOf("]"));
String oneNum = s.substring(s.indexOf(onelen) + onelen.length(),
s.indexOf("]") + ss.indexOf("]"));
if(!Util.isNullOrEmpty(oneNum)){
oneNumList.add(Integer.valueOf(oneNum));
}else{
oneNumList.add(0);
}
}
int oneMax = Collections.max(oneNumList);
for (int i = 0; i <= oneMax; i++) {
JSONObject jobj = new JSONObject();
for (String s : twoList) {
if (s.indexOf(onelen + i + "]") != -1) {
// 转成想要的格式
String ss = one + "[" + i + "][";
String key = s
.substring(s.indexOf(ss) + ss.length(), ss.length()
+ s.substring(ss.length()).indexOf("]"));
String value = s.substring(s.indexOf(key + "]=")
+ key.length() + 2);
// System.out.println("第二层:"+key+":"+value);
jobj.put(key, value);
}else if(s.indexOf(onelen + "]") != -1) {
// 转成想要的格式
String ss = one + "[][";
String key = s
.substring(s.indexOf(ss) + ss.length(), ss.length()
+ s.substring(ss.length()).indexOf("]"));
String value = s.substring(s.indexOf(key + "]=")
+ key.length() + 2);
// System.out.println("第二层:"+key+":"+value);
jobj.put(key, value);
}
}
ja.add(jobj);
}
oneobj.put("two", ja);
json.add(oneobj);
return json.toString();
}