ds_array[471] = { code:"DS471", title:"Certain Methodologies and their Application to Anti-Dumping Proceedings Involving China", origin:"CHN", destination:"USA", start_date:"2013/12/03", subject:"", short_title:"", agreement:"", update_date:"2013/12/03", status:"ST1#2013_12_03", third_party:"", reports_adopted:"false"};
ds_array[470] = { code:"DS470", title:"Anti-Dumping and Countervailing Duty Investigations on Certain Paper Products from Indonesia", origin:"IDN", destination:"PAK", start_date:"2013/11/27", subject:"G112", short_title:"", agreement:"A9_1102#A9_890#A9_855#A9_1106#A9_858#A6_1#A6_536#A6_241#A20_379#A20_425#A20_676#A20_680#A20_543", update_date:"2013/11/27", status:"ST1#2013_11_27", third_party:"", reports_adopted:"false"};等等,里面全是键值对,开始考虑用json,但是json需要封装对象,这些数组里的键又不一致,所以没有办法封装对象,json也就用不成了。而且据我了解键值对应该是“code”:"DS471"这样两边都有双引号的,所以这些数组里又不像键值对,请问各位大神我需要怎么获取里面的数据啊
7 个解决方案
#1
不管用什么获取,你获取之后,都会因为键值不同而导致代码复杂度更高啊。
所以这个并不是不能使用json的原因。
你这本就是数组,直接转成json格式提交就可以了啊,然后后台接收到之后,解析成数组格式。
每一个数组元素都是一个对象。这没有问题的。
唯一的麻烦就是,数组中对象的键值并不是完全相同的,这个问题是避免不了的,你使用任何格式去提交,都会有这个问题存在啊,这个就在后台处理时,麻烦一点呗,
比如使用某个属性时,先判断是不是有这个属性存在,没有就不做处理,这也是结构不同的交互带来的一个麻烦吧。
所以这个并不是不能使用json的原因。
你这本就是数组,直接转成json格式提交就可以了啊,然后后台接收到之后,解析成数组格式。
每一个数组元素都是一个对象。这没有问题的。
唯一的麻烦就是,数组中对象的键值并不是完全相同的,这个问题是避免不了的,你使用任何格式去提交,都会有这个问题存在啊,这个就在后台处理时,麻烦一点呗,
比如使用某个属性时,先判断是不是有这个属性存在,没有就不做处理,这也是结构不同的交互带来的一个麻烦吧。
#2
java 有js 解析器
String fun = "var x= {code:\"DS471\", title:\"Certain Methodologies and their Application to Anti-Dumping Proceedings Involving China\", origin:\"CHN\",";
fun += " destination:\"USA\", start_date:\"2013/12/03\", subject:\"\", short_title:\"\", agreement:\"\", update_date:\"2013/12/03\", status:\"ST1#2013_12_03\", ";
fun += " third_party:\"\", reports_adopted:\"false\"};";
fun += " var obj=\"\";for(var o in x){ obj+=(o+\"-->\"+x[o]+\"|\"); } ";
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine engine = sem.getEngineByExtension("js");
try {
engine.eval(fun);
} catch (ScriptException ex) {
ex.printStackTrace();
}
System.out.println(engine.get("obj"));
#3
您好,我用这个方法试了,但是取出来总是null还有异常,代码如下:
public static void main(String rags[]){
String test=" var ds = { code:\"DS471\", title:\"Certain Methodologies and their Application to Anti-Dumping Proceedings Involving China\", " +
"origin:\"CHN\", destination:\"USA\", start_date:\"2013/12/03\", subject:\"\", short_title:\"\", agreement:\"\"," +
" update_date:\"2013/12/03\", status:\"ST1#2013_12_03\", third_party:\"\", reports_adopted:\"false\"}";
test += " var obj=\"\";for(var o in x){ obj+=(o+\"-->\"+x[o]+\"|\"); } ";
ScriptEngineManager sem=new ScriptEngineManager();
ScriptEngine se = sem.getEngineByExtension("js");
try {
se.eval(test);
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(se.get("obj"));
}
结果和异常如下:
null
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException: missing ; before statement (<Unknown source>#1) in <Unknown source> at line number 1
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:124)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
at com.wanfang.dousact.DoMain6.main(DoMain6.java:47)
还请您指点。。。
public static void main(String rags[]){
String test=" var ds = { code:\"DS471\", title:\"Certain Methodologies and their Application to Anti-Dumping Proceedings Involving China\", " +
"origin:\"CHN\", destination:\"USA\", start_date:\"2013/12/03\", subject:\"\", short_title:\"\", agreement:\"\"," +
" update_date:\"2013/12/03\", status:\"ST1#2013_12_03\", third_party:\"\", reports_adopted:\"false\"}";
test += " var obj=\"\";for(var o in x){ obj+=(o+\"-->\"+x[o]+\"|\"); } ";
ScriptEngineManager sem=new ScriptEngineManager();
ScriptEngine se = sem.getEngineByExtension("js");
try {
se.eval(test);
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(se.get("obj"));
}
结果和异常如下:
null
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException: missing ; before statement (<Unknown source>#1) in <Unknown source> at line number 1
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:124)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
at com.wanfang.dousact.DoMain6.main(DoMain6.java:47)
还请您指点。。。
#4
missing ;
你拷贝2楼允许看看。
" update_date:\"2013/12/03\", status:\"ST1#2013_12_03\", third_party:\"\", reports_adopted:\"false\"} ;";
你拷贝2楼允许看看。
" update_date:\"2013/12/03\", status:\"ST1#2013_12_03\", third_party:\"\", reports_adopted:\"false\"} ;";
#5
4楼神马意思啊???没有看懂。。。。。
#6
少了 分号
;
#7
我又试了,不报错了,怎么输出的是null啊,不过谢谢你哈
顺便再问一句,这些在js里面是数组吗?如果是的话,怎么存的是键值对啊,还有那些中括号里的数组是什么意思啊,不是指数组的长度吧?
var ds_array = new Array();
ds_array[ 471] = { code:"DS471", title:"Certain Methodologies and their
Application to Anti-Dumping Proceedings Involving China",
origin:"CHN", destination:"USA", start_date:"2013/12/03",
subject:"", short_title:"", agreement:"",
update_date:"2013/12/03", status:"ST1#2013_12_03",
third_party:"", reports_adopted:"false"};
ds_array[ 470] = { code:"DS470", title:"Anti-Dumping and Countervailing
Duty Investigations on Certain Paper Products from
Indonesia", origin:"IDN", destination:"PAK",
start_date:"2013/11/27", subject:"G112", short_title:"",
agreement:"A9_1102#A9_890#A9_855#A9_1106#A9_858#A6_1#A6_536
#A6_241#A20_379#A20_425#A20_676#A20_680#A20_543",
update_date:"2013/11/27", status:"ST1#2013_11_27",
third_party:"", reports_adopted:"false"};
顺便再问一句,这些在js里面是数组吗?如果是的话,怎么存的是键值对啊,还有那些中括号里的数组是什么意思啊,不是指数组的长度吧?
var ds_array = new Array();
ds_array[ 471] = { code:"DS471", title:"Certain Methodologies and their
Application to Anti-Dumping Proceedings Involving China",
origin:"CHN", destination:"USA", start_date:"2013/12/03",
subject:"", short_title:"", agreement:"",
update_date:"2013/12/03", status:"ST1#2013_12_03",
third_party:"", reports_adopted:"false"};
ds_array[ 470] = { code:"DS470", title:"Anti-Dumping and Countervailing
Duty Investigations on Certain Paper Products from
Indonesia", origin:"IDN", destination:"PAK",
start_date:"2013/11/27", subject:"G112", short_title:"",
agreement:"A9_1102#A9_890#A9_855#A9_1106#A9_858#A6_1#A6_536
#A6_241#A20_379#A20_425#A20_676#A20_680#A20_543",
update_date:"2013/11/27", status:"ST1#2013_11_27",
third_party:"", reports_adopted:"false"};
#1
不管用什么获取,你获取之后,都会因为键值不同而导致代码复杂度更高啊。
所以这个并不是不能使用json的原因。
你这本就是数组,直接转成json格式提交就可以了啊,然后后台接收到之后,解析成数组格式。
每一个数组元素都是一个对象。这没有问题的。
唯一的麻烦就是,数组中对象的键值并不是完全相同的,这个问题是避免不了的,你使用任何格式去提交,都会有这个问题存在啊,这个就在后台处理时,麻烦一点呗,
比如使用某个属性时,先判断是不是有这个属性存在,没有就不做处理,这也是结构不同的交互带来的一个麻烦吧。
所以这个并不是不能使用json的原因。
你这本就是数组,直接转成json格式提交就可以了啊,然后后台接收到之后,解析成数组格式。
每一个数组元素都是一个对象。这没有问题的。
唯一的麻烦就是,数组中对象的键值并不是完全相同的,这个问题是避免不了的,你使用任何格式去提交,都会有这个问题存在啊,这个就在后台处理时,麻烦一点呗,
比如使用某个属性时,先判断是不是有这个属性存在,没有就不做处理,这也是结构不同的交互带来的一个麻烦吧。
#2
java 有js 解析器
String fun = "var x= {code:\"DS471\", title:\"Certain Methodologies and their Application to Anti-Dumping Proceedings Involving China\", origin:\"CHN\",";
fun += " destination:\"USA\", start_date:\"2013/12/03\", subject:\"\", short_title:\"\", agreement:\"\", update_date:\"2013/12/03\", status:\"ST1#2013_12_03\", ";
fun += " third_party:\"\", reports_adopted:\"false\"};";
fun += " var obj=\"\";for(var o in x){ obj+=(o+\"-->\"+x[o]+\"|\"); } ";
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine engine = sem.getEngineByExtension("js");
try {
engine.eval(fun);
} catch (ScriptException ex) {
ex.printStackTrace();
}
System.out.println(engine.get("obj"));
#3
您好,我用这个方法试了,但是取出来总是null还有异常,代码如下:
public static void main(String rags[]){
String test=" var ds = { code:\"DS471\", title:\"Certain Methodologies and their Application to Anti-Dumping Proceedings Involving China\", " +
"origin:\"CHN\", destination:\"USA\", start_date:\"2013/12/03\", subject:\"\", short_title:\"\", agreement:\"\"," +
" update_date:\"2013/12/03\", status:\"ST1#2013_12_03\", third_party:\"\", reports_adopted:\"false\"}";
test += " var obj=\"\";for(var o in x){ obj+=(o+\"-->\"+x[o]+\"|\"); } ";
ScriptEngineManager sem=new ScriptEngineManager();
ScriptEngine se = sem.getEngineByExtension("js");
try {
se.eval(test);
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(se.get("obj"));
}
结果和异常如下:
null
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException: missing ; before statement (<Unknown source>#1) in <Unknown source> at line number 1
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:124)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
at com.wanfang.dousact.DoMain6.main(DoMain6.java:47)
还请您指点。。。
public static void main(String rags[]){
String test=" var ds = { code:\"DS471\", title:\"Certain Methodologies and their Application to Anti-Dumping Proceedings Involving China\", " +
"origin:\"CHN\", destination:\"USA\", start_date:\"2013/12/03\", subject:\"\", short_title:\"\", agreement:\"\"," +
" update_date:\"2013/12/03\", status:\"ST1#2013_12_03\", third_party:\"\", reports_adopted:\"false\"}";
test += " var obj=\"\";for(var o in x){ obj+=(o+\"-->\"+x[o]+\"|\"); } ";
ScriptEngineManager sem=new ScriptEngineManager();
ScriptEngine se = sem.getEngineByExtension("js");
try {
se.eval(test);
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(se.get("obj"));
}
结果和异常如下:
null
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException: missing ; before statement (<Unknown source>#1) in <Unknown source> at line number 1
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:124)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
at com.wanfang.dousact.DoMain6.main(DoMain6.java:47)
还请您指点。。。
#4
missing ;
你拷贝2楼允许看看。
" update_date:\"2013/12/03\", status:\"ST1#2013_12_03\", third_party:\"\", reports_adopted:\"false\"} ;";
你拷贝2楼允许看看。
" update_date:\"2013/12/03\", status:\"ST1#2013_12_03\", third_party:\"\", reports_adopted:\"false\"} ;";
#5
4楼神马意思啊???没有看懂。。。。。
#6
少了 分号
;
#7
我又试了,不报错了,怎么输出的是null啊,不过谢谢你哈
顺便再问一句,这些在js里面是数组吗?如果是的话,怎么存的是键值对啊,还有那些中括号里的数组是什么意思啊,不是指数组的长度吧?
var ds_array = new Array();
ds_array[ 471] = { code:"DS471", title:"Certain Methodologies and their
Application to Anti-Dumping Proceedings Involving China",
origin:"CHN", destination:"USA", start_date:"2013/12/03",
subject:"", short_title:"", agreement:"",
update_date:"2013/12/03", status:"ST1#2013_12_03",
third_party:"", reports_adopted:"false"};
ds_array[ 470] = { code:"DS470", title:"Anti-Dumping and Countervailing
Duty Investigations on Certain Paper Products from
Indonesia", origin:"IDN", destination:"PAK",
start_date:"2013/11/27", subject:"G112", short_title:"",
agreement:"A9_1102#A9_890#A9_855#A9_1106#A9_858#A6_1#A6_536
#A6_241#A20_379#A20_425#A20_676#A20_680#A20_543",
update_date:"2013/11/27", status:"ST1#2013_11_27",
third_party:"", reports_adopted:"false"};
顺便再问一句,这些在js里面是数组吗?如果是的话,怎么存的是键值对啊,还有那些中括号里的数组是什么意思啊,不是指数组的长度吧?
var ds_array = new Array();
ds_array[ 471] = { code:"DS471", title:"Certain Methodologies and their
Application to Anti-Dumping Proceedings Involving China",
origin:"CHN", destination:"USA", start_date:"2013/12/03",
subject:"", short_title:"", agreement:"",
update_date:"2013/12/03", status:"ST1#2013_12_03",
third_party:"", reports_adopted:"false"};
ds_array[ 470] = { code:"DS470", title:"Anti-Dumping and Countervailing
Duty Investigations on Certain Paper Products from
Indonesia", origin:"IDN", destination:"PAK",
start_date:"2013/11/27", subject:"G112", short_title:"",
agreement:"A9_1102#A9_890#A9_855#A9_1106#A9_858#A6_1#A6_536
#A6_241#A20_379#A20_425#A20_676#A20_680#A20_543",
update_date:"2013/11/27", status:"ST1#2013_11_27",
third_party:"", reports_adopted:"false"};