如何从另一个json中提取json节点。例如,我想获取“公司名称”即“kjh”。但是使用这个json解析器代码我能够获取整个json而不仅仅是comapnt名称。有人可以提供帮助
jsonObject = (JSONObject) new org.json.simple.parser.JSONParser().parse(domainRequest);
final String companyName = (String) jsonObject.get("companyName");
这是Json的内容:
{"companyName":{"Company Name:":"kjh","Address 1:":"kjhhkh","Address 2:":"hkjhkj","Address 3:":"hkjhhkj","Address 4:":"kjhj","Postcode:":898,"Default Email Address:":"kkjkh@y","Company Registration No:":98,"VAT No:":89098,"Website":"http://localhost:9000/#/support/domain/request?formLinkUuid=7f000101-4fdf-160d-814f-dfa60dc80000"}}
{"companyName" : {
"Company Name:":"kjh",
"Address 1:":"kjhhkh",
"Address 2:":"hkjhkj",
"Address 3:":"hkjhhkj",
"Address 4:":"kjhj",
"Postcode:":898,
"Default Email Address:":"kkjkh@y","Company Registration No:":98,
"VAT No:":89098,
"Website":"http://localhost:9000/#/support/domain/request?formLinkUuid=7f000101-4fdf-160d-814f-dfa60dc80000"
}}
答案 0 :(得分:2)
你错过了一步,你实际上得到了一张地图(键值对),使用这张地图得到'WHERE:Tag.tag:LIKE' => '%' .$searchTerm. '%',
'OR:Tag.tag:LIKE' => '%' .$wordOne. '%' .$wordTwo. '%'
company name
<强>输出强>
public static void main(String[] args) throws JSONException {
String domainRequest = "{\"companyName\":{\"Company Name:\":\"kjh\",\"Address 1:\":\"kjhhkh\",\"Address 2:\":\"hkjhkj\",\"Address 3:\":\"hkjhhkj\",\"Address 4:\":\"kjhj\",\"Postcode:\":898,\"Default Email Address:\":\"kkjkh@y\",\"Company Registration No:\":98,\"VAT No:\":89098,\"Website\":\"http://localhost:9000/#/support/domain/request?formLinkUuid=7f000101-4fdf-160d-814f-dfa60dc80000\"}}";
JSONObject jsonObject = new JSONObject(domainRequest);
JSONObject jsonMap = (JSONObject) jsonObject.get("companyName"); // Generates HashMap, key-value pair
String companyName = (String) jsonMap.get("Company Name:"); // from map prepared above get key value
System.out.println(companyName);
}
答案 1 :(得分:0)
你的json格式很奇怪。你应该看一下。
从子属性名称中删除冒号。
String json =
"{\"companyName\" : {\n" +
" \"Company Name:\":\"kjh\",\n" +
" \"Address 1:\":\"kjhhkh\",\n" +
" \"Address 2:\":\"hkjhkj\",\n" +
" \"Address 3:\":\"hkjhhkj\",\n" +
" \"Address 4:\":\"kjhj\",\n" +
" \"Postcode:\":898,\n" +
" \"Default Email Address:\":\"kkjkh@y\",\"Company Registration No:\":98,\n" +
" \"VAT No:\":89098,\n" +
" \"Website\":\"http://localhost:9000/#/support/domain/request?formLinkUuid=7f000101-4fdf-160d-814f-dfa60dc80000\"\n" +
" }}";
JsonElement jsonElement = new Gson().fromJson(json, JsonElement.class);
String companyName = jsonElement.getAsJsonObject().get("companyName").getAsJsonObject().get("Company Name:").getAsString();
System.out.println(companyName);