json 的一些常用例子

需要的包:commons-beanutils-1.8.0.jar  commons-collections.jar  commons-lang.jar  commons-logging.jar   ezmorph-1.0.3.jar    json-lib-2.3-jdk15.jar

 

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://howareyou73.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=package%20com%3B" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. package  com;  
package com;
Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://howareyou73.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=import%20java.util.ArrayList%3B%0Aimport%20java.util.HashMap%3B%0Aimport%20java.util.List%3B%0Aimport%20java.util.Map%3B" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. import  java.util.ArrayList;  
  2. import  java.util.HashMap;  
  3. import  java.util.List;  
  4. import  java.util.Map;  
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://howareyou73.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=import%20net.sf.json.JSONArray%3B%0Aimport%20net.sf.json.JSONObject%3B" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. import  net.sf.json.JSONArray;  
  2. import  net.sf.json.JSONObject;  
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://howareyou73.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=public%20class%20Test%20%7B%0A%26nbsp%3Bpublic%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%26nbsp%3B%26nbsp%3BPerson%20p1%3Dnew%20Person()%3B%0A%26nbsp%3B%26nbsp%3Bp1.setName(%22%E5%BC%A0%E5%B1%B1%22)%3B%0A%26nbsp%3B%26nbsp%3Bp1.setAge(%2220%22)%3B%0A%26nbsp%3B%26nbsp%3BPerson%20p2%3Dnew%20Person()%3B%0A%26nbsp%3B%26nbsp%3Bp2.setName(%22%E6%9D%8E%E5%9B%9B%22)%3B%0A%26nbsp%3B%26nbsp%3Bp2.setAge(%2223%22)%3B%0A%26nbsp%3B%26nbsp%3B%0A%26nbsp%3B%26nbsp%3BList%20list%3Dnew%20ArrayList()%3B%0A%26nbsp%3B%26nbsp%3Blist.add(p1)%3B%0A%26nbsp%3B%26nbsp%3Blist.add(p2)%3B%0A%26nbsp%3B%26nbsp%3B%0A%26nbsp%3B%26nbsp%3BMap%20map%3Dnew%20HashMap()%3B%0A%26nbsp%3B%26nbsp%3Bmap.put(%22aa%22%2C%20%2211%22)%3B%0A%26nbsp%3B%26nbsp%3Bmap.put(%22bb%22%2C%20true)%3B%0A%26nbsp%3B%26nbsp%3Bmap.put(%22cc%22%2C%20new%20String%5B%5D%7B%22hong%22%2C%22li%22%7D)%3B%0A%26nbsp%3B%26nbsp%3BJSONArray%20jaa%3DJSONArray.fromObject(list)%3B%0A%26nbsp%3B%26nbsp%3BJSONArray%20jam%3DJSONArray.fromObject(map)%3B%0A%26nbsp%3B%26nbsp%3BJSONObject%20jo%3DJSONObject.fromObject(map)%3B%0A%26nbsp%3B%26nbsp%3BSystem.out.println(jaa.toString())%3B%0A%26nbsp%3B%26nbsp%3BSystem.out.println(jam.toString())%3B%0A%26nbsp%3B%26nbsp%3BSystem.out.println(jo.toString())%3B%0A%26nbsp%3B%26nbsp%3B%0A%26nbsp%3B%26nbsp%3BString%20str%3D%22%7B%5C%22age%5C%22%3A%5C%2220%5C%22%2C%5C%22name%5C%22%3A%5C%22%E5%BC%A0%E5%B1%B1%5C%22%7D%2C%7B%5C%22age%5C%22%3A%5C%2223%5C%22%2C%5C%22name%5C%22%3A%5C%22%E6%9D%8E%E5%9B%9B%5C%22%7D%22%3B%0A%26nbsp%3B%26nbsp%3BJSONObject%20jsonObject%20%3D%20JSONObject.fromObject(str)%3B%0A%26nbsp%3B%26nbsp%3BObject%20obj%3DJSONObject.toBean(jsonObject)%3B%0A%26nbsp%3B%26nbsp%3B%0A%26nbsp%3B%26nbsp%3BSystem.out.println()%3B%0A%26nbsp%3B%26nbsp%3B%0A%26nbsp%3B%7D%0A%7D%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. public   class  Test {  
  2.  public   static   void  main(String[] args) {  
  3.   Person p1=new  Person();  
  4.   p1.setName("张山" );  
  5.   p1.setAge("20" );  
  6.   Person p2=new  Person();  
  7.   p2.setName("李四" );  
  8.   p2.setAge("23" );  
  9.     
  10.   List list=new  ArrayList();  
  11.   list.add(p1);  
  12.   list.add(p2);  
  13.     
  14.   Map map=new  HashMap();  
  15.   map.put("aa" "11" );  
  16.   map.put("bb" true );  
  17.   map.put("cc" new  String[]{ "hong" , "li" });  
  18.   JSONArray jaa=JSONArray.fromObject(list);  
  19.   JSONArray jam=JSONArray.fromObject(map);  
  20.   JSONObject jo=JSONObject.fromObject(map);  
  21.   System.out.println(jaa.toString());  
  22.   System.out.println(jam.toString());  
  23.   System.out.println(jo.toString());  
  24.     
  25.   String str="{\"age\":\"20\",\"name\":\"张山\"},{\"age\":\"23\",\"name\":\"李四\"}" ;  
  26.   JSONObject jsonObject = JSONObject.fromObject(str);  
  27.   Object obj=JSONObject.toBean(jsonObject);  
  28.     
  29.   System.out.println();  
  30.     
  31.  }  
  32. }  
public class Test {
 public static void main(String[] args) {
  Person p1=new Person();
  p1.setName("张山");
  p1.setAge("20");
  Person p2=new Person();
  p2.setName("李四");
  p2.setAge("23");
  
  List list=new ArrayList();
  list.add(p1);
  list.add(p2);
  
  Map map=new HashMap();
  map.put("aa", "11");
  map.put("bb", true);
  map.put("cc", new String[]{"hong","li"});
  JSONArray jaa=JSONArray.fromObject(list);
  JSONArray jam=JSONArray.fromObject(map);
  JSONObject jo=JSONObject.fromObject(map);
  System.out.println(jaa.toString());
  System.out.println(jam.toString());
  System.out.println(jo.toString());
  
  String str="{\"age\":\"20\",\"name\":\"张山\"},{\"age\":\"23\",\"name\":\"李四\"}";
  JSONObject jsonObject = JSONObject.fromObject(str);
  Object obj=JSONObject.toBean(jsonObject);
  
  System.out.println();
  
 }
}

 

遇到的问题

Exception in thread "main" net.sf.json.JSONException: java.lang.NoSuchMethodException: Property 'age' has no getter method in class 'class com.Person'

解决:声明bean为public class xxx,必须是public,我用默认类型(class xxx)都不行

你可能感兴趣的:(java,json,.net,Flash,Go)