关于json嵌套解析

猫哥 程序代码评论字数 2292阅读7分38秒阅读模式

转载自 http://www.iteye.com/problems/69856

{   
   "fullname": "Sean Kelly",   
   "org": "SK Consulting",   
   "emailaddrs": [   
      {"type": "work", "value": "kelly@seankelly.biz"},   
      {"type": "home", "pref": 1, "value": "kelly@seankelly.tv"}   
   ],   
    "telephones": [   
      {"type": "work", "pref": 1, "value": "+1 214 555 1212"},   
      {"type": "fax", "value": "+1 214 555 1213"},   
      {"type": "mobile", "value": "+1 214 555 1214"}   
   ],   
   "addresses": [   
      {"type": "work", "format": "us",   
       "value": "1234 Main StnSpringfield, TX 78080-1216"},   
      {"type": "home", "format": "us",   
       "value": "5678 Main StnSpringfield, TX 78080-1316"}   
   ],   
    "urls": [   
      {"type": "work", "value": "http://seankelly.biz/"},   
      {"type": "home", "value": "http://seankelly.tv/"}   
   ]   
} 

需要Jackson。http://jackson.codehaus.org/

package jsonparsingtest;

import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;

public class JsonParsingTest {

	public static class Person {
		private String fullname = null;
		private String org = null;
		private List
emailaddrs = new ArrayList
(); private List
telephones = new ArrayList
(); private List
addresses = new ArrayList
(); private List
urls = new ArrayList
(); public String getFullname() { return fullname; } public void setFullname(String fullname) { this.fullname = fullname; } public String getOrg() { return org; } public void setOrg(String org) { this.org = org; } public List
getEmailaddrs() { return emailaddrs; } public void setEmailaddrs(List
emailaddrs) { this.emailaddrs = emailaddrs; } public List
getTelephones() { return telephones; } public void setTelephones(List
telephones) { this.telephones = telephones; } public List
getAddresses() { return addresses; } public void setAddresses(List
addresses) { this.addresses = addresses; } public List
getUrls() { return urls; } public void setUrls(List
urls) { this.urls = urls; } } public static class Address { private String type = null; private String value = null; private String format = null; private int pref = 0; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getFormat() { return format; } public void setFormat(String format) { this.format = format; } public int getPref() { return pref; } public void setPref(int pref) { this.pref = pref; } } public static void main(String[] args) throws Exception { ObjectMapper om = new ObjectMapper(); // 对象就在这里读取。 Person person = om.readValue(System.in, Person.class); // 怎么用,随便。 System.out.println(person.getFullname()); System.out.println(person.getEmailaddrs().get(0).getValue()); } }

继续阅读
猫哥
  • 本文由 发表于 2015-01-20 22:14:07
  • 转载请务必保留本文链接:https://www.588693.com/205.html
安卓通过shell命令启动APP怎么操作 程序代码

安卓通过shell命令启动APP怎么操作

要启动Android应用程序,可以使用adb shell命令,语法如下:adb shell am start -n /。其中,是应用程序的包名,是应用程序的入口Activity名称。 相关问题 and...
Android build.prop简介 程序代码

Android build.prop简介

build.prop位于手机的/system/build.prop中 build.prop记录一些系统设置,是一个属性文件,相当于Windows系统的注册表。 build.prop生成: Make系统...
php程序列表程序 程序代码

php程序列表程序

记录备份一下 H5ai,就是一个文件目录列表程序,可以搭建在服务器上,可以作为下载站点,也可以当成私有云盘。h5ai是一款功能强大 php 文件目录列表程序,不需要数据库,支持在线预览文本、图片、音频...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定