java JSON 執行個體

來源:互聯網
上載者:User
package Test;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import net.sf.json.JSONObject;import net.sf.json.JSONSerializer;import net.sf.json.JsonConfig;public class Jtest {public static void main(String[] args) {HashMap<String, String> hm = new HashMap<String, String>();hm.put("aa", "123aa");hm.put("bb", "123bb");hm.put("cc", "123cc");User user = new User();user.setUsername("hewei");user.setPassword("123");List<User> ul = new ArrayList<User>();ul.add(user);//JSONArray jsona = JSONArray.fromObject("['aa','bb','<q>']");//JSONObject jsono = JSONObject.fromObject(new User());  //json 轉 實體類JSONObject jsonobject = JSONObject.fromObject("{username:'xx',password:'yy'}");JsonConfig jsonConfig = new JsonConfig();jsonConfig.setArrayMode( JsonConfig.MODE_OBJECT_ARRAY );jsonConfig.setRootClass(User.class);User user2 = (User) JSONSerializer.toJava(jsonobject,jsonConfig);System.out.println(user2.getUsername()+"  "+user2.getPassword());  //實體類轉 jsonJSONObject jsonobject2 = JSONObject.fromObject(user);System.out.println(jsonobject2.toString());}} package Test; public class User  {private String username;private String password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.