当前位置: 首页 > 工具软件 > JSON for .NET > 使用案例 >

net.sf.json 转换JSON

蓝昊天
2023-12-01

maven 中的依赖:

<dependency>

     <groupId>net.sf.json-lib</groupId>
     <artifactId>json-lib</artifactId>
     <version>2.2.3</version>
     <classifier>jdk13</classifier>

</dependency>


轻量级的JSON在Java与JScript的转换

public static void main(String[] args) {


//1. List集合转换成json代码


List list = new ArrayList();


list.add( "first" );


list.add( "second" );


JSONArray jsonArray2 = JSONArray.fromObject( list );
System.out.println(jsonArray2);
//2. Map集合转换成json代码


Map map = new HashMap();


map.put("name", "json");


map.put("bool", Boolean.TRUE);


map.put("int", new Integer(1));


map.put("arr", new String[] { "a", "b" });


map.put("func", "function(i){ return this.arr[i]; }");


JSONObject json = JSONObject.fromObject(map);
JSONArray son=json.getJSONArray("arr");
System.out.println(son+son.getString(0));
System.out.println(json);
//3. Bean转换成json代码


//JSONObject jsonObject = JSONObject.fromObject(new JsonBean());


//4. 数组转换成json代码


boolean[] boolArray = new boolean[] { true, false, true };


JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
System.out.println(jsonArray1);
 


//5. 一般数据转换成json代码


JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );
System.out.println(jsonArray3);
System.out.println(jsonArray3.get(0));
System.out.println(jsonArray3.get(1));
System.out.println(jsonArray3.get(2));
}

 类似资料: