码迷,mamicode.com
首页 > 编程语言 > 详细

java解析和拼接json对象

时间:2018-09-13 16:22:12      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:turn   static   ===   字符   string   add   获取对象   vat   world   

  1 /** 
  2 
  3  * @author: helloworlda 
  4 
  5  * @time:2012-1-18  
  6 
  7  * @descript:java拼接和解析json对象    
  8 
  9  * @result:get没测试出来,不知道效率怎么样。 
 10 
 11  */  
 12 
 13 package json;  
 14 
 15 import java.util.ArrayList;  
 16 
 17 import java.util.Iterator;  
 18 
 19 import java.util.List;  
 20 
 21 import net.sf.json.JSONArray;  
 22 
 23 import net.sf.json.JSONObject;  
 24 
 25 /** 
 26 
 27  * 
 28 
 29  */  
 30 
 31 public class TestJson {  
 32 
 33  public static void main(String[] args) {  
 34 
 35   System.out.println("---------------------java拼接json对象----------------------");  
 36 
 37   TestJson ss=new TestJson();  
 38 
 39   ss.viewMagazine();  
 40 
 41   System.out.println("---------------------java解析json对象----------------------");    
 42 
 43   strJsonObj();  
 44 
 45  }  
 46 
 47  /** 
 48 
 49   * 拼json对象 
 50 
 51   */  
 52 
 53  public String viewMagazine(){  
 54 
 55     
 56 
 57   Person person=new Person();    
 58 
 59   person.setBirth("1989-22-11");  
 60 
 61   person.setGrade("07java");  
 62 
 63   person.setName("happ");  
 64 
 65   person.setSex("boy");  
 66 
 67     
 68 
 69   //推荐的杂志的结果集  
 70 
 71   Person person1=new Person();    
 72 
 73   person1.setBirth("1989-22-11");  
 74 
 75   person1.setGrade("07java");  
 76 
 77   person1.setName("helloworlda");  
 78 
 79   person1.setSex("girl");  
 80 
 81    
 82 
 83   List<Person> list=new ArrayList<Person>();  
 84 
 85   list.add(person);  
 86 
 87   list.add(person1);  
 88 
 89    
 90 
 91   JSONObject s=new JSONObject();  
 92 
 93   JSONArray ss=JSONArray.fromObject(list);//构建json数组  
 94 
 95   //往json对象中加值  
 96 
 97   s.put("person", person);//添加对象  
 98 
 99    
100 
101   s.put("personlist", list);//添加数组,list和ss都可以  
102 
103   //s.put("personss", ss);//添加数组,list和ss都可以  
104 
105   s.put("comCount", 3);  
106 
107   System.out.println(s);    
108 
109   return null;   
110 
111  }  
112 
113    
114 
115  /** 
116 
117   * java解析json对象,解析出对象和字符串及数组并遍历出相应的值 
118 
119   */  
120 
121    
122 
123  private static void strJsonObj(){  
124 
125   String json = "{‘name‘: ‘helloworlda‘,‘array‘:[{‘a‘:‘111‘,‘b‘:‘222‘,‘c‘:‘333‘},{‘a‘:‘999‘}],‘address‘:‘111‘,‘people‘:{‘name‘:‘happ‘,‘sex‘:‘girl‘}}";  
126 
127   JSONObject jsonobj=JSONObject.fromObject(json);//将字符串转化成json对象   
128 
129   String name=jsonobj.getString("name");//获取字符串。  
130 
131   JSONArray array=jsonobj.getJSONArray("array");//获取数组  
132 
133   JSONObject obj=jsonobj.getJSONObject("people");//获取对象  
134 
135     
136 
137   System.out.println("===============strJsonObj==================");  
138 
139   System.out.println("jsonobj : "+jsonobj);  
140 
141   System.out.println("array : "+array);  
142 
143   System.out.println("obj : "+obj.getString("name"));  
144 
145     
146 
147   //遍历json对象  
148 
149    Iterator<?> objkey=obj.keys();    
150 
151   while (objkey.hasNext()) {// 遍历JSONObject     
152 
153         String aa2 = (String) objkey.next().toString();     
154 
155        String bb2 = obj.getString(aa2);           
156 
157        System.out.println(aa2+":"+bb2);  
158 
159   }     
160 
161   //遍历数组  
162 
163   for (int i = 0; i < array.size(); i++) {     
164 
165    System.out.println("item "+ i + " :" + array.getString(i));     
166 
167   }  
168 
169  }  
170 
171 }  

 

java解析和拼接json对象

标签:turn   static   ===   字符   string   add   获取对象   vat   world   

原文地址:https://www.cnblogs.com/hello-studio/p/9640585.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!