java json 获取对象名_java代码解析json对象获取json对象属性值

代码如下:

package com.ultrapower.syn.webservice.test;

import org.json.JSONArray;

import org.json.JSONObject;

public class TestJson {

/**

* @param args

*/

public static void main(String[] args) {

String JsonStr = "{data:["

+"{id: ‘1‘, code: ‘app1‘, name: ‘app1‘, classifierId: ‘Root‘, alterationType: ‘‘, parentId: ‘#‘, attributes: {} },"

+"{id: ‘2‘, code: ‘app1_db1‘, name: ‘xx‘, classifierId: ‘Catalog‘, alterationType: ‘‘, parentId: ‘1‘, attributes: {}},"

+"{id: ‘3‘, code: ‘edw‘, name: ‘xx‘, classifierId: ‘Schema‘, alterationType: ‘‘, parentId: ‘2‘, attributes: {}},"

+"{id: ‘4‘, code: ‘Table1‘, name: ‘表1‘, classifierId: ‘Table‘, alterationType: ‘‘, parentId: ‘3‘, attributes: {}},"

+"{id: ‘5‘, code: ‘Column1‘, name: ‘字段1‘, classifierId: ‘Column‘, alterationType: ‘delete‘, parentId: ‘4‘, attributes: {length: 20, dataType: ‘varchar‘}},"

+"{id: ‘6‘, code: ‘Column2‘, name: ‘字段2‘, classifierId: ‘Column‘, alterationType: ‘add‘, parentId: ‘4‘, attributes: {length: 20, dataType: ‘varchar‘}}"

+"]}";

try {

parseJsonToBeanInfo(JsonStr);

} catch (Exception e) {

e.printStackTrace();

}

}

public static void parseJsonToBeanInfo(String JsonInfo) throws Exception {

if(!"".equals(JsonInfo)&&JsonInfo!=null){

//先把String 形式的 JSON 转换位 JSON 对象

JSONObject json = new JSONObject(JsonInfo);

//得到 JSON 属性对象列表

JSONArray jsonArray =json.getJSONArray("data");

//遍历,得到属性值

for (int i = 0; i < jsonArray.length(); i++) {

JSONObject jo = jsonArray.getJSONObject(i);

String id = jo.getString("id");

String name = jo.getString("name");

String parentId = jo.getString("parentId");

String alterationType = jo.getString("alterationType");

System.out.print("id is:"+id);

System.out.print(" name is:"+name);

System.out.print(" parentId is:"+parentId);

System.out.println(" alterationType is:"+alterationType);

}

}

}

}

运行结果如下:

id is:1 name is:app1 parentId is:# alterationType is:

id is:2 name is:xx parentId is:1 alterationType is:

id is:3 name is:xx parentId is:2 alterationType is:

id is:4 name is:表1 parentId is:3 alterationType is:

id is:5 name is:字段1 parentId is:4 alterationType is:delete

id is:6 name is:字段2 parentId is:4 alterationType is:add

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值