There are two kinds of structures for JSON construction:
ObjectAnd
Array
JSON is simply called objects and arrays in JavaScript, so these two structures are objects and arrays of 2 structures that can represent a variety of complex structures.
The following is a JSON format in the project, it is not very complicated, post the parsing method and thinking:
{"Result": true, "Msgtype": 3, "count": +, "BatchId": "hb20140711", "Companyfullname": "Hubei Province network Bo Exhibition Co., Ltd.", "Goodsid": "P002", "Goodsname": "Hubei ge Dian Internet Industry Exposition", "Goodsdes": "3as.jpg", "goodsimg": "/company/download/goods_img/ 9a036-4c39-418d-a1d7-d5dadba66ab3.jpg "," menulist ": [{" title ":" Exhibitor Guide "," Menucontent ":" c.jpg\ "" c2.jpg\ "" C3.jpg\ ""}, {"title": "Exhibitor", "menucontent": "? Exhibitor List 1XXX Investment Co., Ltd. 2XXX Network Technology Co., Ltd. 3XXX Zhai Technology Co., Ltd."},{"title": "Exhibition Overview", "Menucontent": "Exposition Status one, Expo venue and schedule: August 2014 14-15th 08:30-17:00 Launch date: August 16, 2014 08:30-17:00 August 17, 2014 08:30-17:00 August 18, 2014 08:30-14:00 "}]}
Analysis:1. A single entity has a multiple attribute 2. There is a collection in the Entity 3. The collection also has its own properties
Solution: 1. Create entity (entity of commodity information, entity in collection Menulist) 2. list< Collection Menulist entity >3 is created in the commodity entity class. Add Menulist collection to the base product information after parsing it
Commodity entity information: Goodsresultinfo.java
/******************************************************************************* * * Copyright (c) Weaver Info Tech Co. LTD * * Goodsresultinfo * * App.backend.model.GoodsResultInfo.java * todo:file description or class description. * * @author: Gao_chun * @since: September 22, 2014 * @version: 1.0.0 * * @changeLogs: * 1.0.0:first created this class. * ******************************************************************************/package App.backend.model; Import Java.io.serializable;import java.util.list;/** * @author Gao_chun * */public class Goodsresultinfo implements serializable{private static final long serialversionuid = 1L; Private String BatchId; private int count; Private String Companyfullname; Private String goodsdes; Private String Goodsid; Private String goodsimg; Private String Goodsname; Private String menulist; private int msgtype; private Boolean result; Private list<goodsresultitem> Mgoodsresultitem; Menulist Collection//get , set method omitted}
menulist Entity information: Goodsresultitem.java
/******************************************************************************* * * Copyright (c) Weaver Info Tech Co. LTD * * Goodsresultitem * * App.backend.model.GoodsResultItem.java * todo:file description or class description. * * @author: Gao_chun * @since: September 22, 2014 * @version: 1.0.0 * * @changeLogs: * 1.0.0:first created this class. * /package App.backend.model;import java.io.serializable;/** * @author Gao_chun * */public class Goodsresultitem implements serializable{ private String title; Private String menucontent; Get, set method omitted}
Parser Package: Goodsresultparser.java
/******************************************************************************* * * Copyright (c) Weaver Info Tech Co. LTD * * Authnparser * * Web.demo.parse.LoginParser.java * todo:file description or class description. * * @author: Gao_chun * @since: 2014-9-22 * @version: 1.0.0 * * @changeLogs: * 1.0.0:first created this class. * ******************************************************************************/package App.backend.network.parser;import Java.util.arraylist;import Java.util.list;import Org.json.JSONArray;import Org.json.jsonobject;import Android.text.textutils;import App.backend.model.goodsresultinfo;import App.backend.model.goodsresultitem;import app.util.log;/** * @author Gao_chun * */public class goodsresultparser{/* (No N-javadoc) * @see Parsejson (org.json.JSONObject) */public static Goodsresultinfo Parsejson (String object) { LOG.I ("Goodsresultparser----------->", Object); Goodsresultinfo Mgoodsresultinfo; Result Information list&Lt Goodsresultitem> Goodsitemlist;//menulist in the collection Goodsresultitem Mgoodsitem; Menulist Child Entry try {if (Object! = NULL &&! Textutils.isempty (object)) {Jsonobject obj = new Jsonobject (object); Mgoodsresultinfo = new Goodsresultinfo (); Goodsitemlist = new arraylist<goodsresultitem> (); Mgoodsresultinfo.setresult (Obj.getboolean ("result")); Mgoodsresultinfo.setmsgtype (Obj.getint ("Msgtype")); Mgoodsresultinfo.setcount (Obj.getint ("Count")); Mgoodsresultinfo.setbatchid (obj.getstring ("BatchId")); Mgoodsresultinfo.setcompanyfullname (obj.getstring ("Companyfullname")); Mgoodsresultinfo.setgoodsid (obj.getstring ("Goodsid")); Mgoodsresultinfo.setgoodsname (obj.getstring ("Goodsname")); Mgoodsresultinfo.setgoodsdes (obj.getstring ("Goodsdes")); Mgoodsresultinfo.setgoodsimG (Obj.getstring ("goodsimg")); Menulist Jsonarray menulist = Obj.getjsonarray ("Menulist"); if (menulist! = null && menulist.length () > 0) {for (int i = 0; i < Menuli St.length (); i++) {Jsonobject Jsonitem = Menulist.getjsonobject (i); Get each JSON object Mgoodsitem = new Goodsresultitem (); Entry in Menulist mgoodsitem.settitle (jsonitem.getstring ("title")); Mgoodsitem.setmenucontent (jsonitem.getstring ("menucontent")); Goodsitemlist.add (Mgoodsitem); Put in the collection Mgoodsresultinfo.setmgoodsresultitem (goodsitemlist);//Put the collection into the Commodity entity class} } return mgoodsresultinfo; }} catch (Exception e) {e.printstacktrace (); } return null; }}
the final use and results:
Parse result data, pass to page display goodsresultinfo mgoodslist = Goodsresultparser.parsejson (result); Intent resultintent = new Intent (this,goodsdetailsactivity.class); Resultintent.putextra (result,mgoodslist); StartActivity (resultintent);
Results:
Goodsresultinfo [batchid=hb20140711, Count=16, companyfullname= Hubei Province network Bo Exhibition Co., Ltd., goodsdes=<p><a target= "_self" href= "http://www.365yunshang.com/10805" ></A></P>, goodsid=p002, goodsImg=/company/ Download/goods_img/937ea036-4c39-418d-a1d7-d5dadba66ab3.jpg, Goodsname=, Menulist=null, msgType=3, Hubei ge Dian Internet Industry Exposition Result=true, Mgoodsresultitem=[[email protected], [email protected], [email protected]]
Of course, this kind of solution is not necessarily the most perfect, can also introduce third-party open-source jar to complete, but the internal implementation method and principle are similar. (This method is for reference only)
Android Complex JSON parsing