json对象的转换 http://localhost:8080/testhttp/testAction



http://localhost:8080/testhttp/testAction

项目名+请求名

 String str ="{\"name\":\"json\",\"name2\":\"json2\"}";
  JSONObject a = new JSONObject(str);
  System.out.println(a);
  System.out.println(a.toString());
  System.out.println(a.get("name"));
  System.out.println(a.get("name2"));

package com.actions;

import java.util.List;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

public class Test{
 private SortedMap<String,String> parameters = new TreeMap<String,String>();
 
 
 public void test1(){
  System.out.println("进入test1了");
  try{
      //创建连接
   
         HttpClient httpclient = new DefaultHttpClient();
         
      // 设置Cookie的兼容性      
      httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);       
      httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
      httpclient.getParams().setParameter("http.protocol.content-charset","a"); 
      httpclient.getParams().setParameter(HTTP.CONTENT_ENCODING, "a"); 
      httpclient.getParams().setParameter(HTTP.CHARSET_PARAM, "a"); 
      httpclient.getParams().setParameter(HTTP.DEFAULT_PROTOCOL_CHARSET,"a");
      // 15秒连接不上则失败      
      httpclient.getConnectionManager().closeIdleConnections(15, TimeUnit.SECONDS); 
      
      HttpPost httppost = new HttpPost("http://localhost:8080/testhttp/monifirst");//回传地址
      
      final List<NameValuePair> nvps = new ArrayList<NameValuePair>();
      List<NameValuePair> nvps1 = new ArrayList<NameValuePair>();
      
           
     
      nvps.add(new BasicNameValuePair("transNo", "11")); 
            nvps.add(new BasicNameValuePair("transDate",  "111111")); 
         
           
      nvps1.addAll(nvps);
      nvps1.add(new BasicNameValuePair("checkValue", "123"));
      HttpResponse response;
      httppost.setEntity(new UrlEncodedFormEntity(nvps1,"utf-8"));
      response = httpclient.execute(httppost);
     
      String result=EntityUtils.toString(response.getEntity());
      System.out.println(result.equals("true") ? "succcess" : "noSuccess");
      
      

      
      
      
     }catch(Exception e){
      
      
     
     }
 }
  
      
 
 public void monifirst() throws java.text.ParseException  {
     System.out.println("进入monifirst了");
   ActionContext ctx = ActionContext.getContext();
   HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
   HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE);

   SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
   SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   
   String transDate=request .getParameter("transDate");
   
   
   //test123comCode=13031600&payID=201511231818540001&payResult=0000&requestCode=0001&transDate=20151124141300&transNo=201511231818540001
   //     a=comCode=13031600&payID=201511231818540001&payResult=0000&requestCode=0001&transDate=20151124141300&transNo=201511231818540001
   
    String checkValue=request.getParameter("checkValue");
    System.out.println("checkValue="+checkValue);
    String transNo=request.getParameter("transNo");
    System.out.println("transNo="+transNo);
   
    System.out.println("transDate="+transDate);
    PrintWriter printWriter = null;
    try {
     printWriter = response.getWriter();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   
   printWriter.write("true");
   printWriter.flush();
   printWriter.close();
    System.out.println("进入22222了");
  
 }
 
}

package com.test;


import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;


public class Test2 {
        public static void main(String[] args) {
   
  
         String s="<?xml version=\"1.0\" encoding=\"gbk\" ?>" +
            "<smartresult>" +
            "<product type=\"identitycard\">" +
            "<code>440305198012255411</code>" +
            "<location>sdfsdf</location>" +
            "<birthday>19801225</birthday>" +
            "<gender>m</gender></product>" +
            "</smartresult>";
         
         try {
    Document doc= DocumentHelper.parseText(s);
    Node root=doc.getRootElement();
    
       System.out.println(root.getName());
       System.out.println(root.asXML());
     
        
   } catch (DocumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }   
         
        }

        }


你可能感兴趣的:(json)