JSON 解析嵌套json字符串,一个json字符串中嵌套另一个json字符串 (附解析代码)
第一,我从openstack获取server的json数据:
http://192.168.00.00:8888/v2/objectid/servers/serverid
{
"server": {
"status": "ACTIVE",
"updated": "2017-05-10T08:47:15Z",
"hostId": "ec87ea82ff7902c2d64e42fdde74b40465b3d2f1f8efa164264c2e9e",
"OS-EXT-SRV-ATTR:host": "overcloud-compute-1.localdomain",
"addresses": {
"REPOinternalnetwork": [{
"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:ad:e0:ca",
"version": 4,
"addr": "169.254.1.2",
"OS-EXT-IPS:type": "fixed"
}],
"vlan210": [{
"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:2f:97:99",
"version": 4,
"addr": "192.168.210.103",
"OS-EXT-IPS:type": "fixed"
}]
},
"links": [{
"href": "http://192.168.49.25:8774/v2/29ec86a6f17942f49fdc0bcc0748087b/servers/61ff2872-6320-4f53-9b51-1e5e9e174198",
"rel": "self"
},
{
"href": "http://192.168.49.25:8774/29ec86a6f17942f49fdc0bcc0748087b/servers/61ff2872-6320-4f53-9b51-1e5e9e174198",
"rel": "bookmark"
}],
"key_name": "REPO",
"image": {
"id": "243125b4-5cf9-4ad9-827b-37698f6b98f0",
"links": [{
"href": "http://192.168.49.25:8774/29ec86a6f17942f49fdc0bcc0748087b/images/243125b4-5cf9-4ad9-827b-37698f6b98f0",
"rel": "bookmark"
}]
},
"OS-EXT-STS:task_state": null,
"OS-EXT-STS:vm_state": "active",
"OS-EXT-SRV-ATTR:instance_name": "instance-00000200",
"OS-SRV-USG:launched_at": "2017-04-25T12:00:50.000000",
"OS-EXT-SRV-ATTR:hypervisor_hostname": "overcloud-compute-1.localdomain",
"flavor": {
"id": "00061ec1-4405-40fe-87a5-d06191f6826d",
"links": [{
"href": "http://192.168.49.25:8774/29ec86a6f17942f49fdc0bcc0748087b/flavors/00061ec1-4405-40fe-87a5-d06191f6826d",
"rel": "bookmark"
}]
},
"id": "61ff2872-6320-4f53-9b51-1e5e9e174198",
"security_groups": [{
"name": "default"
},
{
"name": "REPOsecuritygroup"
}],
"OS-SRV-USG:terminated_at": null,
"OS-EXT-AZ:availability_zone": "zone1",
"user_id": "1ea47fcca9d5497a8e605bb7ae2a07a6",
"name": "repo01",
"created": "2017-04-25T11:56:49Z",
"tenant_id": "29ec86a6f17942f49fdc0bcc0748087b",
"OS-DCF:diskConfig": "MANUAL",
"os-extended-volumes:volumes_attached": [{
"id": "5e952eab-f3d6-4c67-9471-a698c07e4052"
}],
"accessIPv4": "",
"accessIPv6": "",
"progress": 0,
"OS-EXT-STS:power_state": 1,
"config_drive": "True",
"metadata": {
"auto_evacuation": "true"
}
}
}
http://192.168.00.00:8888/v2/objectid/flavors/flavorID
{
"flavor": {
"name": "IMS_CMREPO",
"links": [{
"href": "http://192.168.49.25:8774/v2/29ec86a6f17942f49fdc0bcc0748087b/flavors/00061ec1-4405-40fe-87a5-d06191f6826d",
"rel": "self"
},
{
"href": "http://192.168.49.25:8774/29ec86a6f17942f49fdc0bcc0748087b/flavors/00061ec1-4405-40fe-87a5-d06191f6826d",
"rel": "bookmark"
}],
"ram": 16384,
"OS-FLV-DISABLED:disabled": false,
"vcpus": 8,
"swap": "",
"os-flavor-access:is_public": true,
"rxtx_factor": 1.0,
"OS-FLV-EXT-DATA:ephemeral": 0,
"disk": 38,
"id": "00061ec1-4405-40fe-87a5-d06191f6826d"
}
}
再通过http://192.168.00.00:8888/v2/objectid/servers/flavorID的flavorID,获得最终的ram的值(最后目的);
这样的嵌套json解析,一般的JSON遍历肯定是搞不定的,所以就转化为javabean来完成;
先用server和flavor分别创建bean类,把flavor作为server的其中一个变量,再把ram作为flavor的一个变量。
import java.io.Serializable;
@SuppressWarnings("serial")
public class ServerBean implements Serializable {
private Flavor flavor;
public Flavor getFlavor() {
return flavor;
}
public void setFlavors(Flavor flavor) {
this.flavor = flavor;
}
@Override
public String toString() {
return flavor.toString();
}
}
两个类都继承序列化
@SuppressWarnings("serial")
public class Flavor implements Serializable {
private String id;
private int ram;
public int getRam() {
return ram;
}
public void setRam(int ram) {
this.ram = ram;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return id;
}
public int getRaw(){
return ram;
}
}
然后在获取到数据的地方进行Gson解析:
String flavor_ids = "";
String str1 = openstackHandler.getCeilometerServers(serverID);
JSONObject json_test = JSONObject.fromObject(str1);
Gson gson = new Gson();
ServerBean serverBean = gson.fromJson(json_test.getString("server"), ServerBean.class);
Flavor result = serverBean.getFlavor();
flavor_ids = result.toString();
String str2 = openstackHandler.getCeilometerFlavors(flavor_ids);
JSONObject json_test2 = JSONObject.fromObject(str2);
Flavor flavors = gson.fromJson(json_test2.getString("flavor"), Flavor.class);
int ram = flavors.getRam();
虽然方法比较笨一点,麻烦一点,但是结果是我想要的就好。