关于普通Json的大部分解析方法。此博客用于个人复习,欢迎批评
———-原生json解析—-
String json1 = "{/"name/":/"张三/",/"age/":20,/"sex/":/"男/"}";String json3 = "{/"bookname/":/"java速成/", /"PRice/":18.6}";JSONObject jsonobj = new JSONObject(json1);Person person = new Person();Book books = new Book();——解析Object—解析完成后放入实体类中—-
String name = jsonobj.getString("name");int age = jsonobj.getInt("age");String sex = jsonobj.getString("sex");person.setName(name);person.setAge(age);person.setSex(sex);JSONObject jsonobj1 = new JSONObject(json3);String bookname= jsonobj1.getString("bookname");double price = jsonobj1.getDouble("price");books.setBookname(bookname);books.setPrice(price);person.setBooks(books);System.out.println(books);System.out.println(person);/*String json2 = "[/"苹果/",/"栗子/",/"西瓜/"]";——-解析JsonArray—
JSONArray jsonarray = new JSONArray(json2);List<String> list = new ArrayList<>();for(int i = 0; i<jsonarray.length();i++){ String str = jsonarray.getString(i); list.add(str);}System.out.println(list);*/————–JsonObject 和JsonArray 的结合解析
String json4 = "{/"name/":/"李雷/", /"age/":16, /"books/":[{/"bookname/":/"Java入门到放弃/", /"price/":18.9}, {/"bookname/":/"C++领进门,从后门跑了/", /"price/":18.9}, {/"bookname/":/"android这个坑有多深/", /"price/":18.9}]}";JSONObject jsonObject = new JSONObject(json4);Student student = new Student();String name = jsonObject.getString("name");int age = jsonObject.getInt("age");List<Book> book = new ArrayList<>();JSONArray jsonArray = jsonObject.getJSONArray("books");for(int i = 0; i<jsonArray.length();i++){ Book books = new Book(); JSONObject jsonObject2 = jsonArray.getJSONObject(i); String bookname = jsonObject2.getString("bookname"); double pirce = jsonObject2.getDouble("price"); books.setBookname(bookname); books.setPicer(pirce); book.add(books);}student.setName(name);student.setAge(age);student.setList(book);System.out.println(student);} ———————–下载Url到String—从接口中下载数据存到一个String类型的变量中
// 解析Url为Stringpublic static String getJSONByURL(String urlStr) {InputStream in = null;ByteArrayOutputStream out = null;try { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); in = conn.getInputStream(); out = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } byte[] bs = out.toByteArray(); return new String(bs);} catch (Exception e) { e.printStackTrace();} finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } }}return null;——————用Gson解析 首先要创建一个实体类,按照接口里面的格式来创建
String json1 = "{/"name/":/"张三/",/"age/":20,/"sex/":/"男/"}";//PersonString json2 = "[/"苹果/", /"梨子/", /"香蕉/"]";//List<String>String json3 = "{/"bookname/":/"Java速成/", /"price/":18.6}";//解析json1Person ps = new Gson().fromJson(json1, Person.class);System.out.println(ps);//解析json2TypeToken<List<String>> tt = new TypeToken<List<String>>(){};List<String> list = new Gson().fromJson(json2, tt.getType());System.out.println(list);//解析json3Book book = new Gson().fromJson(json3, Book.class);System.out.println(book);String json4 = "{/"name/":/"李雷/", /"age/":16, /"books/":[{/"bookname/":/"Java入门到放弃/", /"price/":18.9}, {/"bookname/":/"C++领进门,从后门跑了/", /"price/":18.9}, {/"bookname/":/"android这个坑有多深/", /"price/":18.9}]}";Student studnent = new Gson().fromJson(json4, Student.class);System.out.println(studnent);——-一个Gson 和json结合的例子:荔枝新闻解析 ——–荔枝新闻的接口,首先要用URl下载这个接口的数据到String 的json中
public static final String URL = "http://litchiapi.jstv.com/api/GetFeeds?column=0&PageSize=10&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41";public static void main(String[] args) throws JSONException {//下载JSONString json = HttpUtil.getJSONByURL(URL);//解析JSON --> List<New>List<NewsEntity> newsList = new ArrayList<>();JSONObject jsonObject = new JSONObject(json);String isok = jsonObject.getString("status");if(isok.equals("ok")){ //数据请求成功 JSONArray newsJsonArray = jsonObject.getJSONObject("paramz").getJSONArray("feeds");———先用Json解析,下面再用Gson解析—–实体类一定要按照对应格式创建
for(int i = 0; i < newsJsonArray.length(); i++){ NewsEntity newsEntity = new NewsEntity(); JSONObject newObject = newsJsonArray.getJSONObject(i); //解析ID newsEntity.setId(newObject.getInt("id")); //解析标题 String subject = newObject.getJSONObject("data").getString("subject"); newsEntity.setSubject(subject); //解析简介 String summary = newObject.getJSONObject("data").getString("summary"); newsEntity.setSummary(summary); //解析图片 String cover = newObject.getJSONObject("data").getString("cover"); newsEntity.setCover(cover); //解析时间 String changed = newObject.getJSONObject("data").getString("changed"); newsEntity.setChanged(changed); newsList.add(newsEntity); }—–下面是荔枝新闻的实体类,按照对应格式,然后选取需要的数据来创建
private int id;// 新闻idprivate String subject;// 新闻标题private String summary;// 新闻简介private String cover;// 新闻图片urlprivate String changed;// 新闻的最后修改时间public int getId() {return id;}public void setId(int id) {this.id = id;}public String getSubject() {return subject;}public void setSubject(String subject) {this.subject = subject;}public String getSummary() {return summary;}public void setSummary(String summary) {this.summary = summary;}public String getCover() {return cover;}public void setCover(String cover) {this.cover = cover;}public String getChanged() {return changed;}public void setChanged(String changed) {this.changed = changed;}@Overridepublic String toString() {return "NewsEntity [id=" + id + ", subject=" + subject + ", summary=" + summary + ", cover=" + cover + ", changed=" + changed + "]";}新闻热点
疑难解答