1. Simply put key-value pairs manually into JSONObject, and then put into JSONArray object
List<Article> al = articleMng.find(f); System.out.println(al.size()); HttpServletResponse hsr = ServletActionContext.getResponse(); if(null == al){ return ; } for(Article a : al){ System.out.println(a.getId()+a.getDescription()+a.getTitle()); } JSONArray json = new JSONArray(); for(Article a : al){ JSONObject jo = new JSONObject(); jo.put("id", a.getId()); jo.put("title", a.getTitle()); jo.put("desc", a.getDescription()); json.put(jo); } try { System.out.println(json.toString()); hsr.setCharacterEncoding("UTF-8"); hsr.getWriter().write(json.toString()); } catch (IOException e) { e.printStackTrace(); }The above code JSONArray is the org.json.JSONArray package introduced
The static method of JSONArray under net.sf.json package: fromObject(list) This is the way to convert JSON quickly on the Internet, but for Hibernate cascading operations associated objects, this method will report an error. If the cascading configuration in the mapping file is removed, it will be fine.
In addition, the requirement for list is that the elements in it are strings or objects, otherwise JSON will not know what data you want.
<many-to-one name="cmsent" column="comment_tid" not-null="false" cascade="delete">
However, cascading operations still have to exist after all, otherwise the data will be redundant and redundant in the future.
The solution is: JSONArray subMsgs = JSONArray.fromObject(object, config);
JsonConfig config = new JsonConfig(); config.setJsonPropertyFilter(new PropertyFilter() { public boolean apply(Object arg0, String arg1, Object arg2) { if (arg1.equals("article") ||arg1.equals("fans")) { return true; } else { return false; } } }); Description: Provides a filtering function. If an associated object is encountered, it will automatically filter out and will not execute the associated object. Here I post the code for configuring relation mapping in hibernate to help understand:
<!-- Configure the relationship between topics and groups --> <many-to-one name="article" column="article_id"/> <!-- Configure the relationship between topic posts and posts replied --> <set name="subMessages" table="sub_message" inverse="true" cascade="all" lazy="false" order-by="date asc"> <key column="theme_id" /> <one-to-many /> </set>
Summarize:
1. JSONArray subMsgs = JSONArray.fromObject(subMessages, config); where config is optional. When the above situation occurs, you can configure the config parameters. If there is no such requirement above, you can directly use the fromObject(obj) method. What it converts is the standard json object format data, as follows:
{["attr", "content", ...}, ...]}
2. JSONObject jTmsg = JSONObject.fromObject(themeMessage, config); This is specially used to parse standard pojo, or map object. Needless to say, the format of the pojo object is, the form of map is like this {"str", "str"}.
---------------------------------------------------------- 分割-------------------------------------------------------------------------------------------
For JSONArray and JSON, I used to vomit before! ! !
bean
package com.nubb.bean;import java.io.Serializable;public class Person implements Serializable{ private static final long serialVersionUID = 1L; private String name; private int age; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } JsonUtil
package com.nubb.test;import java.io.IOException;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.StandardOpenOption;import java.util.ArrayList;import java.util.List;import com.alibaba.fastjson.JSON;import com.nubb.bean.Person;public class JSONSerializer { private static final String DEFAULT_CHARSET_NAME = "UTF-8"; public static <T> String serialize(T object) { return JSON.toJSONString(object); } public static <T> T deserialize(String string, Class<T> clz) { return JSON.parseObject(string, clz); } public static <T> T load(Path path, Class<T> clz) throws IOException { return deserialize( new String(Files.readAllBytes(path), DEFAULT_CHARSET_NAME), clz); } public static <T> void save(Path path, T object) throws IOException { if (Files.notExists(path.getParent())) { Files.createDirectories(path.getParent()); } Files.write(path, serialize(object).getBytes(DEFAULT_CHARSET_NAME), StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); } public static void main(String[] args) { Person person1 = new Person(); person1.setAddress("address"); person1.setAge(11); person1.setName("amao"); Person person2 = new Person(); person2.setAddress("address"); person2.setAge(11); person2.setName("amao"); List<Person> lp = new ArrayList<Person>(); lp.add(person1); lp.add(person2); System.out.println(serialize(lp)); } }Output:
The code copy is as follows:
[{"address":"address","age":11,"name":"amao"},{"address":"address","age":11,"name":"amao"}]
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.