常用的json解析

Java JSON解析实例
import java.util.Random;

import org.json.JSONException;
import org.json.JSONObject;

public class parseJson {
    public static String parse(String json_str,String name) {
        // TODO Auto-generated method stub
        JSONObject jsonObject = null;
        String jsondata=null;
        try {
            jsonObject = new JSONObject(json_str);
             jsondata=jsonObject.getJSONObject("responseBody").getString(name);
            jsondata=jsonObject.getString(name);

             
        
             

            
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return jsondata;
    }
    public static void main(String[] args){

//    String json_str="{\"total\":920,\"data\":[{\"ID\":\"634\",\"Name\":\"于东\"},{\"ID\":\"822\",\"Name\":\"于祎\"},{\"ID\":\"782\",\"Name\":\"于燕\"},{\"ID\":\"636\",\"Name\":\"于玲\"},{\"ID\":\"841\",\"Name\":\"于浩\"},{\"ID\":\"383\",\"Name\":\"于娟\"}]}";
//     String json_str1="{\"memberId\":101024,\"requestTime\":1467267073593,\"service\":\"queryAccount\"}";
     //String requestTime="requestTime";
//    String str2="{\"icegroup\" : {\"result\" : {\"errorCode\" : \"1\",\"errorMessage\" : \"\u767b\u5f55\u6210\u529f\"},\"user\" : {\"uid\" : \"0001\",\"location\" : \"china\",\"name\" : \"Steven\"},\"notice\" : {\"atmeCount\" : \"0\",\"msgCount\" : \"0\",}}}";
    String str3="{\"responseBody\":{\"responseCode\":\"gateway11\",\"responseMsg\":\"JSON\"}}";    
    String str4="{\"responseBody\":{\"paymentId\":1319,\"responseCode\":\"000\"},\"sign\":\"iND4SPiIG+EdPldXq9p5UmTqAthCHnpa0hWEM+B/cmUQzoMtQCdQFnlmcADYyvaY5S7V06Bq2QTPGNbsJjUFPTNf2gmtZ2ds1UZ/K3Ru7rwzVR6LUUo4s5rh4jRqTKpqw79O7woXebof1TRuLplRA8flIxAOv5zyBbvKntqI3qw=\"}";
    String str5="{\"paymentId\":1323,\"responseCode\":\"000\"}";
    String str6="{\"cards\":[{\"bankCode\":\"ICBC\",\"bankName\":\"工商银行\",\"brands\":[],\"cardNum\":\"622202*********2222\",\"categoryCode\":\"debit\",\"dayLimit\":5,\"enable\":false,\"id\":631,\"onceLimit\":5,\"toolCode\":\"quickPay\"}],\"mergeId\":846,\"orderAmount\":1000000,\"orderDescribe\":\"钱包充值\",\"orderId\":846,\"payerBalance\":0,\"protocolTitle\":\"充值商户用户协议《充值商户用户协议》\",\"protocolUrl\":\"http://www.baidu.com\",\"responseCode\":\"000\",\"responseMsg\":\"操作成功\"}";
    
    
//    String request=parse(str6,"mergeId");
    JSONObject jsonObject = null;
    try {
        jsonObject = new JSONObject(str6);
        String request=jsonObject.getString("mergeId");
        System.out.println("ddddddddddd   "+request);
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
 //    jsondata=jsonObject.getJSONObject("responseBody").getString(name);
    

    
        System.out.println(str5);
        String responseCode =null;
        
        JSONObject obj;
        try {
            obj = new JSONObject(str4);
            String strNew=obj.getString("responseBody");
            JSONObject objNew = new JSONObject(strNew);  
             responseCode = objNew.getString("responseCode");
            
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
        System.out.println("666"+responseCode);

        System.out.println(System.currentTimeMillis());
        System.out.println(System.currentTimeMillis()+10000);
        long m=System.currentTimeMillis();
        System.out.println(m);
        
    /*    String a="13";
        
        Random  random = new  Random();
                String number1 = a+  random.nextInt(1000000000);
               
                System.out.println(number1);*/
        
        Random  random = new  Random();
        String a="13";
        int b=random.nextInt(100000000)+100000000;
         String number1 = a+  String.valueOf(b);
        
         System.out.println(number1);
         
         
         
               
        
        
        

        
        
        
    }

}

 

转载于:https://my.oschina.net/cctester/blog/737700

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值