JSON学习笔记一 —— 一些与移动端交互产生JSON数据的方法
/**
* 测试的返回JSon方法,正式的不会用
* @author MrHandler
* @param reqCode
* @param joinStr
* @return
*/
public static String getBasicJsonStrTest(int reqCode,String joinStr){
StringBuffer jsonStr = new StringBuffer();
jsonStr.append("{\"outParam\":{");
jsonStr.append("\"reqCode\":"+"\""+reqCode+"\",");
jsonStr.append("\"reqBak\":"+"\""+CodeSettingsSkil.getErrorCodeDesc(String.valueOf(reqCode))+"\",");
jsonStr.append("\"respTime\":"+"\""+WebServiceValidate.getCurrentStr()+"\","+joinStr);
jsonStr.append("}}");
return jsonStr.toString();
}
/**
* 返回基本的JSon格式的信息
* 示例
{
"outParam": {
"reqCode": "551291",
"reqBak": "特约绑定必须先绑定设备",
"respTime": "2014-12-17 13:52:53"
}
}
* @param reqCode 返回码
* @return
*/
public static String getBasicJsonStr(int reqCode,String bak){
StringBuffer jsonStr = new StringBuffer();
jsonStr.append("{\"outParam\":{");
jsonStr.append("\"reqCode\":"+"\""+reqCode+"\",");
if(bak != null && !"".equals(bak)){
jsonStr.append("\"reqBak\":"+"\""+bak+"\",");
}else{
jsonStr.append("\"reqBak\":"+"\""+CodeSettingsSkil.getErrorCodeDesc(String.valueOf(reqCode))+"\",");
}
jsonStr.append("\"respTime\":"+"\""+WebServiceValidate.getCurrentStr()+"\"");
jsonStr.append("}}");
return jsonStr.toString();
}
/**
* 生成JSon
* 示例
{
"outParam": {
"reqCode": "551291",
"reqBak": "特约绑定必须先绑定设备",
"respTime": "2014-12-17 13:52:53",
"life": {
"x": "1",
"y": "2",
"lifeList": [
{
"goodsId": "id1",
"goodsTitle": "标题1",
"goodsImg": "图片1",
"goodsCount": "1"
},
{
"goodsId": "id2",
"goodsTitle": "标题2",
"goodsImg": "图片2",
"goodsCount": "2"
}
]
}
}
}
* @param reqCode 返回码
* @param fields 返回字段
* @param lists 返回集合
* @param clz 对象
* @supFields 集合要新增的字段 可以为空
* @supFieldsVal 对应集合要新增的字段值 可以为空
* @return
* @throws Exception
*/
public static String getJsonStr(int reqCode,Object[] fields,List<?> lists,Class<?> clz,Object[] supFields,Object[] supFieldsVal) throws Exception{
StringBuffer jsonStr = new StringBuffer();
jsonStr.append("{\"outParam\":{");
jsonStr.append("\"reqCode\":"+"\""+reqCode+"\",");
jsonStr.append("\"reqBak\":"+"\""+CodeSettingsSkil.getErrorCodeDesc(String.valueOf(reqCode))+"\",");
jsonStr.append("\"respTime\":"+"\""+WebServiceValidate.getCurrentStr()+"\",");
jsonStr.append("\"life\":{");
//对象信息
if(supFields != null && supFields.length> 0){
for (int i = 0; i < supFields.length; i++) {
if(i == supFields.length - 1){
if(lists != null && lists.size() > 0){
jsonStr.append("\""+supFields[i]+"\":"+"\""+supFieldsVal[i]+"\",");
}else{
jsonStr.append("\""+supFields[i]+"\":"+"\""+supFieldsVal[i]+"\"");
}
}else{
jsonStr.append("\""+supFields[i]+"\":"+"\""+supFieldsVal[i]+"\",");
}
}
}
if(lists != null && lists.size() > 0){
jsonStr.append("\"lifeList\":[");
for (int i=0;i<lists.size();i++) {
jsonStr.append("{");
Object val = lists.get(i);
for (int j = 0; j < fields.length; j++) {//字段
String methodStr = fields[j].toString();
Method m = clz.getDeclaredMethod("get"+methodStr.substring(0,1).toUpperCase() + methodStr.substring(1));
Object rel = m.invoke(val);//对象调用值
if(rel == null){//针对获取的值进行处理
rel = "";
}
if(j == fields.length -1){
jsonStr.append("\""+methodStr+"\":"+"\""+rel+"\"");
}else{
jsonStr.append("\""+methodStr+"\":"+"\""+rel+"\",");
}
}
if(i == lists.size()-1){
jsonStr.append("}");
}else{
jsonStr.append("},");
}
}
jsonStr.append("]");
}
jsonStr.append("}}}");
return jsonStr.toString();
}
/**
* @param reqCode 返回码
* @param ObjectJson 对象JSON
* @return
* @throws Exception
*/
public static String getJsonStr(int reqCode,Object object) throws Exception{
StringBuffer jsonStr = new StringBuffer();
jsonStr.append("{\"outParam\":{");
jsonStr.append("\"reqCode\":"+"\""+reqCode+"\",");
jsonStr.append("\"reqBak\":"+"\""+CodeSettingsSkil.getErrorCodeDesc(String.valueOf(reqCode))+"\",");
jsonStr.append("\"respTime\":"+"\""+WebServiceValidate.getCurrentStr()+"\",");
if(object!= null){
jsonStr.append("\"life\":"+toJSONString(object));
}
jsonStr.append("}}");
return jsonStr.toString();
}
/**
* 对象里面包含集合
* @author MrHandler
* @param reqCode 返回码
* @param listFields 返回的字段
* @param lists 数据
* @param listClzs 实体类Class
* @supFields 集合要新增的字段 可以为空
* @supFieldsVal 对应集合要新增的字段值 可以为空
* @return
* @throws Exception
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static String getJsonStrByObjectToList(int reqCode,List<Object[]> listFields,List<?> lists,List<Class<?>> listClzs,
Object[] supFields,Object[] supFieldsVal) throws Exception{
StringBuffer jsonStr = new StringBuffer();
jsonStr.append("{\"outParam\":{");
jsonStr.append("\"reqCode\":"+"\""+reqCode+"\",");
jsonStr.append("\"reqBak\":"+"\""+CodeSettingsSkil.getErrorCodeDesc(String.valueOf(reqCode))+"\",");
jsonStr.append("\"respTime\":"+"\""+WebServiceValidate.getCurrentStr()+"\",");
jsonStr.append("\"life\":{");
//对象信息
if(supFields != null && supFields.length> 0){
for (int i = 0; i < supFields.length; i++) {
if(i == supFields.length - 1){
if(lists != null && lists.size() > 0){
jsonStr.append("\""+supFields[i]+"\":"+"\""+supFieldsVal[i]+"\",");
}else{
jsonStr.append("\""+supFields[i]+"\":"+"\""+supFieldsVal[i]+"\"");
}
}else{
jsonStr.append("\""+supFields[i]+"\":"+"\""+supFieldsVal[i]+"\",");
}
}
}
if(lists != null && lists.size() > 0){
jsonStr.append("\"lifeList\":[");
for (int i=0;i<lists.size();i++) {
jsonStr.append("{");
Object val = lists.get(i);
Object[] fields = listFields.get(0);//主字段
Class clz = listClzs.get(0); //主对象
for (int j = 0; j < fields.length; j++) {//字段
String methodStr = fields[j].toString();
Method m = clz.getDeclaredMethod("get"+methodStr.substring(0,1).toUpperCase() + methodStr.substring(1));
Object rel = m.invoke(val);//对象调用值
if(rel == null){//针对获取的值进行处理
rel = "";
}
if(rel instanceof List<?>){//对象里面包含集合
int f =0;
Iterator iterator = ((List) rel).listIterator();
jsonStr.append("\""+methodStr+"\":[");
while (iterator.hasNext()) {
jsonStr.append("{");
Object val2=iterator.next();//对象
for (int k = 1; k < listClzs.size(); k++) {
Class subClz = listClzs.get(k);
if(val2.getClass().equals(subClz)){//需要反射的子对象
//遍历对象里面的集合
/* String className = subClz.getName();
className = className.substring(0,1).toLowerCase()+className.substring(1)+"List";*/
Object[] subFields = listFields.get(k);
for (int h = 0;h < subFields.length; h++) {
String methodStr2 = subFields[h].toString();
Method m2 = subClz.getDeclaredMethod("get"+methodStr2.substring(0,1).toUpperCase() + methodStr2.substring(1));
Object rel2 = m2.invoke(val2);//对象调用值
if(rel2==null){
rel2 = "";
}
if(h == subFields.length -1){
jsonStr.append("\""+methodStr2+"\":"+"\""+rel2+"\"");
}else{
jsonStr.append("\""+methodStr2+"\":"+"\""+rel2+"\",");
}
}
}
}
//要判断是否是最后一个字段
if( f==((List) rel).size()-1){
//要判断是否是最后一个字段
jsonStr.append("}");//这是否有多个信息
}else{
//要判断是否是最后一个字段
jsonStr.append("},");//这是否有多个信息
}
f++;
}
jsonStr.append("]");//这是否有多个信息
}else{
if(j == fields.length -1){
jsonStr.append("\""+methodStr+"\":"+"\""+rel+"\"");
}else{
jsonStr.append("\""+methodStr+"\":"+"\""+rel+"\",");
}
}
}
if(i == lists.size()-1){
jsonStr.append("}");
}else{
jsonStr.append("},");
}
}
jsonStr.append("]");
}
jsonStr.append("}}}");
return jsonStr.toString();
}
/**
* 针对所有的公共的生成JSon格式
* @author MrHandler
* @param reqCode 返回码
* @param mapFields 返回字段
* @param mapLists 返回的集合
* @param mapClzs 返回的映射类
* @param supFields 集合要新增的字段 可以为空
* @param supFieldsVal 对应集合要新增的字段值 可以为空
* @return
* @throws Exception
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static String getJsonStrByMap(int reqCode,Map<String, Object[]> mapFields,Map<String, List> mapLists,Map<String,Class> mapClzs,Object[] supFields,Object[] supFieldsVal) throws Exception{
StringBuffer jsonStr = new StringBuffer();
jsonStr.append("{\"outParam\":{");
jsonStr.append("\"reqCode\":"+"\""+reqCode+"\",");
jsonStr.append("\"reqBak\":"+"\""+CodeSettingsSkil.getErrorCodeDesc(String.valueOf(reqCode))+"\",");
jsonStr.append("\"respTime\":"+"\""+WebServiceValidate.getCurrentStr()+"\",");
jsonStr.append("\"life\":{");
//对象信息
if(supFields != null && supFields.length> 0){
for (int i = 0; i < supFields.length; i++) {
if(i == supFields.length - 1){
if(mapLists != null && mapLists.size() > 0){
jsonStr.append("\""+supFields[i]+"\":"+"\""+supFieldsVal[i]+"\",");
}else{
jsonStr.append("\""+supFields[i]+"\":"+"\""+supFieldsVal[i]+"\"");
}
}else{
jsonStr.append("\""+supFields[i]+"\":"+"\""+supFieldsVal[i]+"\",");
}
}
}
if(mapLists != null && mapLists.size() > 0){
int count=0;
for(Object mn: mapLists.keySet()){
String jsonElement =mn.toString();
jsonStr.append("\""+jsonElement+"\":[");
List lists = mapLists.get(mn);
for (int i=0;i<lists.size();i++) {
jsonStr.append("{");
Object val = lists.get(i);
Object[] fields = mapFields.get(jsonElement);
Class clz = mapClzs.get(jsonElement);
for (int j = 0; j < fields.length; j++) {//字段
String methodStr = fields[j].toString();
Method m = clz.getDeclaredMethod("get"+methodStr.substring(0,1).toUpperCase() + methodStr.substring(1));
Object rel = m.invoke(val);//对象调用值
if(rel == null){//针对获取的值进行处理
rel = "";
}
if(j == fields.length -1){
jsonStr.append("\""+methodStr+"\":"+"\""+rel+"\"");
}else{
jsonStr.append("\""+methodStr+"\":"+"\""+rel+"\",");
}
}
if(i == lists.size()-1){
jsonStr.append("}");
}else{
jsonStr.append("},");
}
}
count++;
if(count == mapLists.size()){
jsonStr.append("]");
}else{
jsonStr.append("],");
}
}
}
jsonStr.append("}}}");
return jsonStr.toString();
}
public static String getEncoding(String str) {
String encode = "GB2312";
try {
if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是GB2312
String s = encode;
return s; //是的话,返回“GB2312“,以下代码同理
}
} catch (Exception exception) {
}
encode = "ISO-8859-1";
try {
if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是ISO-8859-1
String s1 = encode;
return s1;
}
} catch (Exception exception1) {
}
encode = "UTF-8";
try {
if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是UTF-8
String s2 = encode;
return s2;
}
} catch (Exception exception2) {
}
encode = "GBK";
try {
if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是GBK
String s3 = encode;
return s3;
}
} catch (Exception exception3) {
}
return ""; //如果都不是,说明输入的内容不属于常见的编码格式。
}
/**
*
* @param param 请求参数
* @param reqCode 请求码
* @param arr 需要验证的参数
* @param encType 验签方式 md5 1001 rsa 1002
* @return
* @throws JSONException
* @throws UnsupportedEncodingException
*/
@SuppressWarnings("unused")
public static Map<String,String> reqJsonStr(String reqCode[],String arr[],String encType,HttpServletRequest request) throws JSONException, UnsupportedEncodingException{
MallPublicMethod method = new MallPublicMethod();
String met = request.getMethod();
if("GET".equals(met)){
return returnParam(new String[]{"result", "msg"}, new String[]{"1003", String.valueOf(CodeSettings.MALL_INTERFACE_RESPONSECODE551000)});
}
request.setCharacterEncoding("UTF-8");
String inParam =request.getParameter("encryptionParam");
//String str1 =TransferDecUtil.byteArrayToHexString(inParam.getBytes());
String sign = request.getParameter("sign");
if(inParam ==null || "".equals(inParam) || sign==null || "".equals(sign)){
return returnParam(new String[]{"result", "msg"}, new String[]{"1003", String.valueOf(CodeSettings.MALL_INTERFACE_RESPONSECODE551002)});
}
if(arr != null && arr.length>0){
StringBuffer str = new StringBuffer();
for (int i = 0; i < arr.length; i++) {
str.append(arr[i]);
}
}
try{
JSONObject jsonObject = new JSONObject(inParam);
//inParam=inParam.replace("\\n", "\\\\n");
String code = jsonObject.get("reqCode").toString();
String memberNo = jsonObject.get("memberNo").toString();
String reqTime = jsonObject.get("reqTime").toString();
String osName = jsonObject.get("osName").toString();
String version = jsonObject.get("version").toString();
// String sign = jsonObject.get("sign").toString();
// 判断客户端请求方式
String reqMethod = request.getMethod().toLowerCase();
// 验证请求参数是否为空
int checkReqParam = checkReqParam(code, inParam, sign, reqCode, osName,memberNo, reqMethod);
if (1001!=checkReqParam) {
return returnParam(new String[]{"result", "msg","memberNo","osName"}, new String[]{"1002", String.valueOf(checkReqParam),memberNo,osName});
}
//memberNo = convertMemberNo(memberNo);
String memPhone ="";
String memArr[] =convertMemberNoNew(memberNo);
memberNo=memArr[2].toString();
if("1002".equals(memArr[0].toString())){
memPhone =memArr[1].toString();
}
// if(isType!=1001){
if(true){
//如果是加密的要先解密
// 解密出来的请求数据
String encryptionData = null;
if ("1001".equals(encType)) {
// MD5方式 加密
encryptionData = method.serverMd5Verify(inParam,memberNo, sign);
} else if ("1002".equals(encType)) { // Rsa方式加密
encryptionData = method.serverDecAndSign(inParam,memberNo, osName, sign);
}
if ("1002".equals(encryptionData)) {
return returnParam(new String[]{"result", "msg","memberNo","osName"}, new String[]{"1002", String.valueOf(CodeSettings.MALL_INTERFACE_RESPONSECODE551037),memberNo,osName});
// 验签失败
} else if ("1003".equals(encryptionData)) {
return returnParam(new String[]{"result", "msg","memberNo","osName"}, new String[]{"1002", String.valueOf(CodeSettings.MALL_INTERFACE_RESPONSECODE551035),memberNo,osName});
// 解密失败
} else if ("1004".equals(encryptionData)) {
return returnParam(new String[]{"result", "msg","memberNo","osName"}, new String[]{"1002", String.valueOf(CodeSettings.MALL_INTERFACE_RESPONSECODE551036),memberNo,osName});
}else if ("1005".equals(encryptionData)) {
return returnParam(new String[]{"result", "msg","memberNo","osName"}, new String[]{"1002", String.valueOf(CodeSettings.MALL_INTERFACE_RESPONSECODE551046),memberNo,osName});
}
// 验证会员账号
int checkMemberNo = MallPublicMethod.checkMemberNoJson(memberNo);
if (1001 !=checkMemberNo) {
return returnParam(new String[]{"result", "msg","memberNo","osName"}, new String[]{"1002", String.valueOf(checkMemberNo),memberNo,osName});
}
// 验证版本号
String checkVersion = MallPublicMethod.checkVersionJson(osName, version);
if (!"1001".equals(checkVersion)) {
return returnParam(new String[]{"result", "msg","memberNo","osName"}, new String[]{"1002", String.valueOf(checkVersion),memberNo,osName});
}
}
JSONObject reqParam=null;
//判断入参是否一致
if(arr != null && arr.length>0 ){
inParam=inParam.replace("\\n", "\\\\n");
jsonObject=null;
jsonObject = new JSONObject(inParam);
reqParam = jsonObject.getJSONObject("reqParam");
// String str1 =TransferDecUtil.byteArrayToHexString(reqParam.toString().getBytes());
for (int i = 0; i < arr.length; i++) {
String val=reqParam.get(arr[i]).toString();
if(val==null){
return returnParam(new String[]{"result", "msg","memberNo","osName"}, new String[]{"1002", String.valueOf(CodeSettings.MALL_INTERFACE_RESPONSECODE551011),memberNo,osName});
}
}
}
String[] keyArray = new String[30];
String[] valArray = new String[30];
keyArray[0] = "result";
valArray[0] = "1001";
keyArray[1] = "msg";
valArray[1] = "";
keyArray[2] = "version";
valArray[2] = version;
keyArray[3] = "memberNo";
valArray[3] = memberNo;
keyArray[4] = "osName";
valArray[4] = osName;
keyArray[5] = "sign";
valArray[5] = sign;
keyArray[6] = "reqCode";
valArray[6] = code;
keyArray[7] = "loginType";
valArray[7] = memArr[0].toString();
keyArray[8] = "memPhone";
valArray[8] = memPhone;
if(arr != null && arr.length>0){
int i = 9;
for (String string : arr) {
keyArray[i] = string;
valArray[i] = reqParam.get(string).toString();
i++;
}
}
return returnParam(keyArray, valArray);
}catch (Exception e) {
e.printStackTrace();
return returnParam(new String[]{"result", "msg"}, new String[]{"1003", String.valueOf(CodeSettings.MALL_INTERFACE_RESPONSECODE551567)});
}
}
public static Map<String, String> returnParam(String[] key, String[] values){
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < values.length; i++) {
map.put(key[i], values[i]);
}
return map;
}
/**
* 验证请求参数是否为空
* @param reqCode
* @param encParam
* @param sign
* @param reqCodeLocal
* @param osName
* @param memberNo
* @return
*/
public static int checkReqParam(String reqCode , String encParam , String sign ,
String reqCodeLocal[] , String osName , String memberNo, String reqMethod){
if(reqCode == null || "".equals(reqCode)){
return CodeSettings.MALL_INTERFACE_RESPONSECODE551011 ;
}
if(encParam == null || "".equals(encParam)){
return CodeSettings.MALL_INTERFACE_RESPONSECODE551011;
}
if(sign == null || "".equals(sign)){
return CodeSettings.MALL_INTERFACE_RESPONSECODE551011 ;
}
int i = 0;
for (String string : reqCodeLocal) {
if(reqCode.equals(string)){
i++;
}
}
if(i == 0){
return CodeSettings.MALL_INTERFACE_RESPONSECODE551012;
}
if(osName == null || "".equals(osName)){
return CodeSettings.MALL_INTERFACE_RESPONSECODE551012;
}
if(memberNo == null || "".equals(memberNo)){
return CodeSettings.MALL_INTERFACE_RESPONSECODE551012;
}
if(!"1001".equals(osName) && !"1002".equals(osName) && !"1003".equals(osName) && !"1004".equals(osName)){
return CodeSettings.MALL_INTERFACE_RESPONSECODE551012;
}
if (!"post".equals(reqMethod)) {
return CodeSettings.MALL_INTERFACE_RESPONSECODE551039;
}
return 1001;
}
private static NodeList initStart(String protocolXML, String tagName) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //实例化一个文档构建器工厂
DocumentBuilder builder = factory.newDocumentBuilder(); //通过文档构建器工厂获取一个文档构建器
Document doc = builder.parse(new InputSource(new StringReader(protocolXML))); //通过文档通过文档构建器构建一个文档实例
Element root = doc.getDocumentElement();
NodeList n = root.getElementsByTagName(tagName); //获取所有名字为 tagName 的节点
return n;
}
/**
* 公共的节点处理信息
* @param n
* @param i
* @return
*/
private static String[] publicNode(NodeList n, int i) {
Node book = n.item(i);
NodeList nl2 = book.getChildNodes();//此处值得注意,在DOM解析时会将所有回车都视为 n 节点的子节点。
String[] obj = new String[nl2.getLength()];
for (int j = 0; j < nl2.getLength(); j++) {
Node n2 = nl2.item(j);
NodeList nms = n2.getChildNodes();
String val = nms.item(0).getTextContent();
obj[j] = val;
}
return obj;
}
/**
* 读取商品保障信息
* @author MrHandler
* @param xml
* @return
*/
public static List<Guarantee> readGuaranteeXML(String xml) {
List<Guarantee> lists;
try {
lists = new ArrayList<Guarantee>();
NodeList n = initStart(xml, "guarantee");
if (n == null) {
return null;
}
for (int i = 0; i < n.getLength(); i++) {
String[] obj = publicNode(n, i);
lists.add(new Guarantee(Integer.parseInt(obj[0]), obj[1], obj[2]));
}
}catch (Exception e) {
e.printStackTrace();
return null;
}
return lists;
}
/**
* 获取商品详情对象
* @author MrHandler
* @param xmlMsg
* @return
*/
public static Object[] getGoodsDeatil(String xmlMsg){
Object[] returnObject = new Object[2];
Goods g = new Goods();
int goodsId1 = xmlMsg.indexOf("<goodsId>");
int goodsId2 = xmlMsg.indexOf("</goodsId>");
String goodsId = xmlMsg.substring(goodsId1+"<goodsId>".length(), goodsId2);
g.setGoodsId(goodsId);
int oldStateStr1 = xmlMsg.indexOf("<oldStateStr>");
int oldStateStr2 = xmlMsg.indexOf("</oldStateStr>");
String oldStateStr = xmlMsg.substring(oldStateStr1+"<oldStateStr>".length(), oldStateStr2);
g.setOldStateStr(oldStateStr);
int goodsTitle1 = xmlMsg.indexOf("<goodsTitle>");
int goodsTitle2 = xmlMsg.indexOf("</goodsTitle>");
String goodsTitle = xmlMsg.substring(goodsTitle1+"<goodsTitle>".length(), goodsTitle2);
g.setGoodsTitle(goodsTitle);
int goodsName1 = xmlMsg.indexOf("<goodsName>");
int goodsName2 = xmlMsg.indexOf("</goodsName>");
String goodsName = xmlMsg.substring(goodsName1+"<goodsName>".length(), goodsName2);
g.setGoodsName(goodsName);
int prefecture1 = xmlMsg.indexOf("<prefecture>");
int prefecture2 = xmlMsg.indexOf("</prefecture>");
String prefecture = xmlMsg.substring(prefecture1+"<prefecture>".length(), prefecture2);
g.setPrefecture(prefecture);
int goodsImg1 = xmlMsg.indexOf("<goodsImg>");
int goodsImg2 = xmlMsg.indexOf("</goodsImg>");
String goodsImg = xmlMsg.substring(goodsImg1+"<goodsImg>".length(), goodsImg2);
g.setGoodsImg(goodsImg);
int typeHot1 = xmlMsg.indexOf("<typeHot>");
int typeHot2 = xmlMsg.indexOf("</typeHot>");
String typeHot = xmlMsg.substring(typeHot1+"<typeHot>".length(), typeHot2);
g.setTypeHot(typeHot);
int goodsCount1 = xmlMsg.indexOf("<goodsCount>");
int goodsCount2 = xmlMsg.indexOf("</goodsCount>");
String goodsCount = xmlMsg.substring(goodsCount1+"<goodsCount>".length(), goodsCount2);
g.setGoodsCount(goodsCount);
int stroeCount1 = xmlMsg.indexOf("<stroeCount>");
int stroeCount2 = xmlMsg.indexOf("</stroeCount>");
String stroeCount = xmlMsg.substring(stroeCount1+"<stroeCount>".length(), stroeCount2);
g.setStroeCount(stroeCount);
int collectCount1 = xmlMsg.indexOf("<collectCount>");
int collectCount2 = xmlMsg.indexOf("</collectCount>");
String collectCount = xmlMsg.substring(collectCount1+"<collectCount>".length(), collectCount2);
g.setCollectCount(collectCount);
int goodsBrandName1 = xmlMsg.indexOf("<goodsBrandName>");
int goodsBrandName2 = xmlMsg.indexOf("</goodsBrandName>");
String goodsBrandName = xmlMsg.substring(goodsBrandName1+"<goodsBrandName>".length(), goodsBrandName2);
g.setGoodsBrandName(goodsBrandName);
int goodsRemark1 = xmlMsg.indexOf("<goodsRemark>");
int goodsRemark2 = xmlMsg.indexOf("</goodsRemark>");
String goodsRemark = xmlMsg.substring(goodsRemark1+"<goodsRemark>".length(), goodsRemark2);
g.setGoodsRemark(goodsRemark);
int isSell1 = xmlMsg.indexOf("<isSell>");
int isSell2 = xmlMsg.indexOf("</isSell>");
String isSell = xmlMsg.substring(isSell1+"<isSell>".length(), isSell2);
g.setIsSell(isSell);
int sellerMemberNo1 = xmlMsg.indexOf("<sellerMemberNo>");
int sellerMemberNo2 = xmlMsg.indexOf("</sellerMemberNo>");
String sellerMemberNo = xmlMsg.substring(sellerMemberNo1+"<sellerMemberNo>".length(), sellerMemberNo2);
g.setSellerMemberNo(sellerMemberNo);
int shopName1 = xmlMsg.indexOf("<shopName>");
int shopName2 = xmlMsg.indexOf("</shopName>");
String shopName = xmlMsg.substring(shopName1+"<shopName>".length(), shopName2);
g.setShopName(shopName);
int logo1 = xmlMsg.indexOf("<logo>");
int logo2 = xmlMsg.indexOf("</logo>");
String logo = xmlMsg.substring(logo1+"<logo>".length(), logo2);
g.setLogo(logo);
int isUploadAttachment1 = xmlMsg.indexOf("<isUploadAttachment>");
int isUploadAttachment2 = xmlMsg.indexOf("</isUploadAttachment>");
String isUploadAttachment = xmlMsg.substring(isUploadAttachment1+"<isUploadAttachment>".length(), isUploadAttachment2);
g.setIsUploadAttachment(isUploadAttachment);
int isGifts1 = xmlMsg.indexOf("<isGifts>");
int isGifts2 = xmlMsg.indexOf("</isGifts>");
String isGifts = xmlMsg.substring(isGifts1+"<isGifts>".length(), isGifts2);
g.setIsGifts(isGifts);
int giftState1 = xmlMsg.indexOf("<giftState>");
int giftState2 = xmlMsg.indexOf("</giftState>");
String giftState = xmlMsg.substring(giftState1+"<giftState>".length(), giftState2);
g.setGiftState(giftState);
int storeCity1 = xmlMsg.indexOf("<storeCity>");
int storeCity2 = xmlMsg.indexOf("</storeCity>");
String storeCity = xmlMsg.substring(storeCity1+"<storeCity>".length(), storeCity2);
g.setStoreCity(storeCity);
int storeProvince1 = xmlMsg.indexOf("<storeProvince>");
int storeProvince2 = xmlMsg.indexOf("</storeProvince>");
String storeProvince = xmlMsg.substring(storeProvince1+"<storeProvince>".length(), storeProvince2);
g.setStoreProvince(storeProvince);
int isPrestore1 = xmlMsg.indexOf("<isPrestore>");
int isPrestore2 = xmlMsg.indexOf("</isPrestore>");
String isPrestore = xmlMsg.substring(isPrestore1+"<isPrestore>".length(), isPrestore2);
g.setIsPrestore(isPrestore);
int hasBill1 = xmlMsg.indexOf("<hasBill>");
if(hasBill1 !=-1){
int hasBill2 = xmlMsg.indexOf("</hasBill>");
String hasBill = xmlMsg.substring(hasBill1+"<hasBill>".length(), hasBill2);
g.setHasBill(hasBill);
}
int expressType1 = xmlMsg.indexOf("<expressType>");
if(expressType1 != -1){
int expressType2 = xmlMsg.indexOf("</expressType>");
String expressType = xmlMsg.substring(expressType1+"<expressType>".length(), expressType2);
g.setExpressType(expressType);
int freight1 = xmlMsg.indexOf("<freight>");
int freight2 = xmlMsg.indexOf("</freight>");
String freight = xmlMsg.substring(freight1+"<freight>".length(), freight2);
g.setFreight(freight);
}
int memberprice1 = xmlMsg.indexOf("<memberprice>");
int memberprice2 = xmlMsg.indexOf("</memberprice>");
String memberprice = xmlMsg.substring(memberprice1+"<memberprice>".length(), memberprice2);
g.setMemberPrice(memberprice);
int specialPrice1 = xmlMsg.indexOf("<specialPrice>");
int specialPrice2 = xmlMsg.indexOf("</specialPrice>");
String specialPrice = xmlMsg.substring(specialPrice1+"<specialPrice>".length(), specialPrice2);
g.setSpecialPrice(specialPrice);
returnObject[0] = g; //商品详情对象
int guarantee1 = xmlMsg.indexOf("<guarantee>");
int guarantee2 = xmlMsg.lastIndexOf("</guarantee>");
String guarantees = xmlMsg.substring(guarantee1, guarantee2+"</guarantee>".length());
guarantees = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>"+guarantees+"</root>";
List<Guarantee> lists = readGuaranteeXML(guarantees);
returnObject[1] = lists; //商品保障
return returnObject;
}
/**
* 转换会员账号
* @param memberNo
* @return
*/
public static String convertMemberNo(String memberNo){
if(ConvertUtil.isNumeric(memberNo)){//如果是纯数字会员
MemberInfoDao dao = new MemberInfoDao();
dao.setDataBaseType(FieldsDB.THREEWEIDU_PEPOS_BUSINESS);
try {
Object[] memberObj = dao.custFieldFind("MEMBERINFO_WEBSERVICEGETPHONE_SELECT", new Object[]{memberNo}, 2);
if(memberObj != null){
return memberObj[1].toString();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return memberNo;
}
public static String[] convertMemberNoNew(String memberNo) {
String arr[]= new String[3];
//如果是纯数字会员
arr[0]="1001"; //是否是手机号码1001 否 1002是
if(ConvertUtil.isNumeric(memberNo)){
arr[0]="1002";
arr[1]=memberNo;
}
String mem= convertMemberNo(memberNo);
arr[2]=mem;
return arr;
}
/**
*
* fastjson 转换
* @param object
* @return
*/
public static String toJSONString(Object object){
return JSON.toJSONString(object);
}
JSON学习笔记一 —— 一些与移动端交互产生JSON数据的方法的更多相关文章
- 2.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:
转自:https://www.cnblogs.com/ssslinppp/p/4528892.html 个人认为,使用@ResponseBody方式来实现json数据的返回比较方便,推荐使用. 摘要 ...
- HTML+CSS学习笔记(5)- 与浏览者交互,表单标签
HTML+CSS学习笔记(5)- 与浏览者交互,表单标签 1.使用表单标签,与用户交互 网站怎样与用户进行交互?答案是使用HTML表单(form).表单是可以把浏览者输入的数据传送到服务器端,这样服务 ...
- SpringBoot学习笔记(10):使用MongoDB来访问数据
SpringBoot学习笔记(10):使用MongoDB来访问数据 快速开始 本指南将引导您完成使用Spring Data MongoDB构建应用程序的过程,该应用程序将数据存储在MongoDB(基于 ...
- 3.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:
转自:https://www.cnblogs.com/ssslinppp/p/4528892.html 概述 在文章:<[Spring学习笔记-MVC-3]SpringMVC返回Json数据-方 ...
- 1.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:
转自:https://www.cnblogs.com/ssslinppp/p/4528892.html [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://w ...
- Binder学习笔记(九)—— 服务端如何响应Test()请求 ?
从服务端代码出发,TestServer.cpp int main() { sp < ProcessState > proc(ProcessState::self()); sp < I ...
- Json学习笔记
一.昨天内容回顾 创建ajax对象 a) 主流浏览器 new XMLHttpRequest(); b) IE浏览器 new ActiveXObject("Msxml2.XMLHTTP. ...
- Newtonsoft.Json学习笔记
Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库(下载地址http://json.codeplex.com/). 下面是Json序列化和反序列化的简单封装: /// & ...
- Ajax+JSON学习笔记(二)
来源:http://www.imooc.com/learn/250 readyState属性 0:请求未初始化,open还没有调用 1:服务器连接已建立,open已经调用了 2:请求已接受,也就是接收 ...
随机推荐
- 5.SVM核函数
核函数(Kernels) 定义 1.1 (核或正定核) 设是中的一个子集,称定义在上的函数是核函数,如果存在一个从到Hilbert空间的映射 使得对任意的,都成立.其中表示Hilbert空间中的内积. ...
- 浅析MySQL基于ROW格式的二进制日志
上文分析的二进制日志实际上是基于STATEMENT格式的,下面我们来看看基于ROW格式的二进制日志,毕竟,两者对应的binlog事件类型也不一样,同时,很多童鞋反映基于ROW格式的二进制日志无法查到原 ...
- Vertica 导出数据测试用例
需求:构建简单的测试用例,完成演示Vertica导出数据的功能. 测试用例:导出test业务用户t_jingyu表中的数据. 一.初始化测试环境 二.导出数据 2.1 vsql命令说明帮助 2.2 导 ...
- 用js触发CSS3-transition过渡动画
用js触发CSS3-transition过渡动画 经过这几天的工作,让我进一步的了解到CSS3的强大,原本许多需要js才能实现的动画效果,现在通过CSS3就能轻易实现了,但是CSS3也有自身的不足,例 ...
- C# 文件下载 : WinINet
在 C# 中,除了 WebClient 我们还可以使用一组 WindowsAPI 来完成下载任务.这就是 Windows Internet,简称 WinINet.本文通过一个 demo 来介绍 Win ...
- Redis分布式集群几点说道
原文地址:http://www.cnblogs.com/verrion/p/redis_structure_type_selection.html Redis分布式集群几点说道 Redis数据量日益 ...
- 在node.js中,使用基于ORM架构的Sequelize,操作mysql数据库之增删改查
Sequelize是一个基于promise的关系型数据库ORM框架,这个库完全采用JavaScript开发并且能够用在Node.JS环境中,易于使用,支持多SQL方言(dialect),.它当前支持M ...
- bzoj1503--treap
这道题和一般的题目不同,A和S操作要修改所有节点.所以定义基准d,每个节点的工资是它的值+d,这样就能完成所有操作. I k:将值为k-d的节点插入treap A k:将d加上k S k:将d减去k, ...
- ListFragment的使用
ListFragment继承了Fragment,顾名思义,ListFragment是一种特殊的Fragment,它包含了一个ListView,在ListView里面显示数据. 1. MainActiv ...
- 解决ngnix服务器上的Discuz!x2.5 Upload Error:413错误
1.修改php.ini sudo nano /etc/php5/fpm/php.ini #打开php.ini找到并修改以下的参数,目的是修改上传限制 max_execution_time = 900 ...