JSON解析嵌套json字符串,⼀个json字符串中嵌套另⼀个json
字符串(附解析代码)
第⼀,我从openstack获取server的json数据:
{
"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": "192.168.49.25:8774/v2/29ec86a6f17942f49fdc0bcc0748087b/servers/61ff2872-6320-4f53-9b51-1e5e9e174198",
"rel": "self"
},
{
"href": "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": "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": "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"
}
}
}
第⼆,我从openstack获取flavors的json数据:
{
"flavor": {
"name": "IMS_CMREPO",
"links": [{
"href": "192.168.49.25:8774/v2/29ec86a6f17942f49fdc0bcc0748087b/flavors/00061ec1-4405-40fe-87a5-d06191f6826d",            "rel": "self"
},
{
"href": "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"
}
}
先通过的server ID(已知),获得server的json数据,从⽽得到flavorID;
这样的嵌套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() {
String();
}
}
两个类都继承序列化
@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 = CeilometerServers(serverID);
JSONObject json_test = JSONObject.fromObject(str1);
Gson gson = new Gson();
ServerBean serverBean = gson.fromJson(String("server"), ServerBean.class);  Flavor result = Flavor();
flavor_ids = String();
String str2 = CeilometerFlavors(flavor_ids);
JSONObject json_test2 = JSONObject.fromObject(str2);
Flavor flavors = gson.fromJson(String("flavor"), Flavor.class);
int ram = Ram();
虽然⽅法⽐较笨⼀点,⿇烦⼀点,但是结果是我想要的就好。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。