1. key value 쌍을 수동으로 jsonobject에 넣고 jsonarray 객체에 넣습니다.
목록 <article> al = articlemng.find (f); System.out.println (al.size ()); httpservletresponse hsr = servletactionContext.getResponse (); if (null == al) {return; } for (기사 a : al) {system.out.println (a.getid ()+a.getDescription ()+a.getTitle ()); } JSONARRAY JSON = NEW JSONARRAY (); for (기사 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 (). 쓰기 (json.tostring ()); } catch (ioexception e) {e.printstacktrace (); }위의 코드 jsonarray는 org.json.jsonarray 패키지가 소개되었습니다
net.sf.json 패키지 아래에 jsonarray의 정적 메소드 : FromObject (list) 이것은 인터넷에서 JSON을 빠르게 변환하는 방법이지만, 최대 절전 모스 케이딩 작업 관련 개체의 경우이 방법은 오류를보고합니다. 매핑 파일의 계단식 구성이 제거되면 괜찮습니다.
또한 목록의 요구 사항은 그 요소가 문자열 또는 객체라는 것입니다. 그렇지 않으면 JSON은 원하는 데이터를 알지 못할 것입니다.
<다중 이름 = "CMSENT"열 = "COMMING_TID"NOT-NULL = "FALLE"CASCADE = "DELETE">
그러나 계단식 작업은 여전히 존재해야하며, 그렇지 않으면 데이터가 중복되고 중복됩니다.
해결책은 다음과 같습니다.
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;}}); 설명 : 필터링 기능을 제공합니다. 관련 객체가 발생하면 자동으로 필터링되며 관련 객체를 실행하지 않습니다. 여기서 나는 이해를 돕기 위해 최대 절전 모드에서 관계 매핑 구성을위한 코드를 게시합니다.
<!-주제와 그룹 간의 관계를 구성-> <다중 하나 이름 = "article"column = "article_id" /> <!-주제 게시물과 게시물 사이의 관계를 구성합니다-> <submessages "table ="sub_message "inverse ="true "cascade ="false "order-by ="dy column = "key column ="airlover = "sub_message"inverse = "sub_message"inverse = <key column = "to. </set>
요약 :
1. 여기서 구성은 선택 사항입니다. 위의 상황이 발생하면 구성 매개 변수를 구성 할 수 있습니다. 위에 그러한 요구 사항이 없으면 FromObject (OBJ) 메소드를 직접 사용할 수 있습니다. 변환하는 것은 다음과 같이 표준 JSON 객체 형식 데이터입니다.
{[ "attr", "content", ...}, ...]}}
2. 이것은 표준 pojo 또는 맵 객체를 구문 분석하는 데 특별히 사용됩니다. 말할 것도없이, 포조 객체의 형식은 맵의 형태는이 { "str", "str"}과 같습니다.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------.
JSONARRAY와 JSON의 경우, 나는 전에 구토를했습니다! ! !
콩
package com.nubb.bean; import java.io.serializable; 공공 계급 사람은 시리얼이즈 가능 {private static final long serialversionuid = 1L; 개인 문자열 이름; 사적인 int 연령; 개인 문자열 주소; 공개 문자열 getName () {return name; } public void setName (문자열 이름) {this.name = 이름; } public int getage () {반환 연령; } public void 설정 (int Age) {this.age = age; } public String getAddress () {return address; } public void setAddress (문자열 주소) {this.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.json; com.nubb.bean.person; public class jsonserializer {private static final String default_charset_name = "utf-8"; public static <t> 문자열 Serialize (t 객체) {return json.tojsonstring (Object); } public static <t> t deserialize (문자열 문자열, 클래스 <t> clz) {return json.parseobject (string, clz); } public static <t> t load (경로 경로, 클래스 <t> clz)는 ioexception {return deserialize (new String (files.readallBytes (path), default_charset_name), clz); } public static <t> void save (path path, t 객체)는 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 perient1 = new person (); person1.setAddress ( "주소"); 개인 1. 세트 (11); person1.setname ( "amao"); Person2 = 새로운 사람 (); person.2.setAddress ( "주소"); person.2.setage (11); person.2.setname ( "amao"); List <person> lp = new ArrayList <Person> (); lp.add (person1); lp.add (person2); System.out.println (Serialize (lp)); }}산출:
코드 사본은 다음과 같습니다.
[{ "주소": "주소", "Age": 11, "Name": "amao"}, { "address": "address", "age": 11, "name": "amao"}]
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.