java后台解析json字符串

1.以下maven依赖需要一起加载

      
     net.sf.json-lib     
     json-lib      
     2.4   
     jdk15   
 

    commons-lang
    commons-lang
    2.6


    commons-httpclient
    commons-httpclient
    3.1


    commons-beanutils
    commons-beanutils
    1.8.3


    commons-collections
    commons-collections
    3.2


    commons-logging
    commons-logging
    1.1.1


    net.sf.ezmorph
    ezmorph
    1.0.6

    
    net.sf.json-lib    
    json-lib-ext-spring    
    1.0.2
2 解析json字符串

package com.jusfoun.util;

import java.util.Iterator;

import org.junit.Test;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Dtest {

	@Test
	public void ddtest(){
		String jsonStr = "{\"id\":\"3\",\"name\":\"bob\",\"pass\":\"123\"}";
		JSONObject jsonObj = JSONObject.fromObject(jsonStr);
		System.out.println(jsonObj.get("name"));
		String arrStr = "[{\"id\":\"3\",\"name\":\"bob\",\"pass\":\"123\"},{\"id\":\"4\",\"name\":\"lancy\",\"pass\":\"134\"}]";
		JSONArray jsonArr = JSONArray.fromObject(arrStr);
		System.out.println(jsonArr.getJSONObject(1).get("name"));
		Iterator it = jsonArr.iterator();
		while(it.hasNext()){
			JSONObject obj = (JSONObject)it.next();
			System.out.println("name:"+obj.get("name")+" pass:"+obj.get("pass"));
		}
	}
}
 
  




你可能感兴趣的:(java)