6
回答
用java程序获取js中的数组
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

各位大虾,我现在要用java程序获取js中一部分数组的数据,请问有什么方法,数组如下:

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"这样两边都有双引号的,所以这些数组里又不像键值对,请问各位大神我需要怎么获取里面的数据啊

<无标签>
举报
struts_hibe...
发帖于4年前 6回/3K+阅
共有6个答案 最后回答: 4年前

周五闲着,赚点辛苦分吧。刚好项目也这种场景。主要就是一个jsp,一个action.主要用到了jackson。至于json键值对引号的问题,比较新的规范要求都带引号,为了兼容性最好规范写吧。

getJsonData.jsp

<%@page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> 
<%
String path = request.getContextPath();	
%>
<script src="<%=path%>/resources/assets/js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
var ds_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"};

var str = '['+ JSON.stringify(ds_array[470]) +',' + JSON.stringify(ds_array[471]) +']';

$.ajax({
	url: "<%=path%>/user/getJsonDataTest_getJsonDataArray",
	async:true,
	data:"postListStr="+str,
	type:"POST",
	dataType:"text",
	error:function(){
	},
	success:function(response){
			alert(response);
	}
});
</script>
GetJsonDataTestAction.java
package com.bjhit.eranges.actions.test;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.codehaus.jackson.map.ObjectMapper;

import com.bjhit.eranges.actions.test.entity.PostBean;
import com.bjhit.eranges.actions.test.entity.PostBeanWrapper;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 获取json数据
 * 
 * @author Administrator
 * 
 */
public class GetJsonDataTestAction extends ActionSupport implements
		ServletRequestAware {
	private HttpServletRequest httpServletRequest;
	private List<PostBean> postList;

	public String getJsonDataArray() {
		String postListStr = httpServletRequest.getParameter("postListStr");
		postListStr = "{\"postList\":" + postListStr + "}";

		try {
			// 将前台json字符串转换为对象
			ObjectMapper mapper = new ObjectMapper();
			PostBeanWrapper postBeanWrapper = mapper.readValue(postListStr,
					PostBeanWrapper.class);
			postList = postBeanWrapper.getPostList();
		} catch (Exception e) {
		}
		return "getJsonDataArray";
	}

	@Override
	public void setServletRequest(HttpServletRequest req) {
		this.httpServletRequest = req;

	}

	public List<PostBean> getPostList() {
		return postList;
	}

	public void setPostList(List<PostBean> postList) {
		this.postList = postList;
	}
}
实体对象PostBean.java PostBeanWrapper.java
package com.bjhit.eranges.actions.test.entity;

public class PostBean {
	private String code;
	private String title;
	private String origin;
	private String destination;
	private String start_date;
	private String subject;
	private String short_title;
	private String agreement;
	private String update_date;
	private String status;
	private String third_party;
	private String reports_adopted;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getOrigin() {
		return origin;
	}

	public void setOrigin(String origin) {
		this.origin = origin;
	}

	public String getDestination() {
		return destination;
	}

	public void setDestination(String destination) {
		this.destination = destination;
	}

	public String getStart_date() {
		return start_date;
	}

	public void setStart_date(String start_date) {
		this.start_date = start_date;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getShort_title() {
		return short_title;
	}

	public void setShort_title(String short_title) {
		this.short_title = short_title;
	}

	public String getAgreement() {
		return agreement;
	}

	public void setAgreement(String agreement) {
		this.agreement = agreement;
	}

	public String getUpdate_date() {
		return update_date;
	}

	public void setUpdate_date(String update_date) {
		this.update_date = update_date;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getThird_party() {
		return third_party;
	}

	public void setThird_party(String third_party) {
		this.third_party = third_party;
	}

	public String getReports_adopted() {
		return reports_adopted;
	}

	public void setReports_adopted(String reports_adopted) {
		this.reports_adopted = reports_adopted;
	}

}
package com.bjhit.eranges.actions.test.entity;

import java.util.List;

import com.bjhit.eranges.entity.AddMember4RoleParam;

public class PostBeanWrapper {
	private List<PostBean> postList;

	public List<PostBean> getPostList() {
		return postList;
	}

	public void setPostList(List<PostBean> postList) {
		this.postList = postList;
	}
	
}
action配置如下:
<action name="getJsonDataTest_*" class="com.bjhit.eranges.actions.test.GetJsonDataTestAction" method="{1}">
			<result name="getJsonDataArray" type="json">
				<param name="includeProperties">postList.*</param>
				<param name="excludeNullProperties">true</param>
			</result>
		</action>




顺便再问一句,这些在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"};

顶部