2 * Copyright 2016 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.vfc.nfvo.resmanagement.common.util;
19 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import net.sf.json.JSONArray;
24 import net.sf.json.JSONException;
25 import net.sf.json.JSONObject;
29 * Json Utility Class.<br/>
34 * @version VFC 1.0 Sep 10, 2016
36 public final class JsonUtil {
38 private static final Logger LOG = LoggerFactory.getLogger(StringUtil.class);
40 private static final int TYPE_STRING = 0;
42 private static final int TYPE_INT = 1;
44 private static final int TYPE_JSONA = 2;
46 private static final int TYPE_JSONO = 3;
48 private static final int TYPE_LONG = 4;
55 * Get Json Field String.<br>
62 public static String getJsonFieldStr(JSONObject jsonObj, String fieldName) {
63 return (String)getJsonFieldObject(jsonObj, fieldName, TYPE_STRING);
68 * Get Json Field Integer.<br>
75 public static Integer getJsonFieldInt(JSONObject jsonObj, String fieldName) {
76 return (Integer)getJsonFieldObject(jsonObj, fieldName, TYPE_INT);
81 * Get Json Field array.<br>
88 public static JSONArray getJsonFieldArr(JSONObject jsonObj, String fieldName) {
89 return (JSONArray)getJsonFieldObject(jsonObj, fieldName, TYPE_JSONA);
94 * Get Json Field Json.<br>
101 public static JSONObject getJsonFieldJson(JSONObject jsonObj, String fieldName) {
102 return (JSONObject)getJsonFieldObject(jsonObj, fieldName, TYPE_JSONO);
107 * Get Json Field Long.<br>
114 public static Long getJsonFieldLong(JSONObject jsonObj, String fieldName) {
115 return (Long)getJsonFieldObject(jsonObj, fieldName, TYPE_LONG);
120 * Get Json Field Object.<br>
128 private static Object getJsonFieldObject(JSONObject jsonObj, String fieldName, int classType) {
130 if(null != jsonObj && jsonObj.has(fieldName)) {
131 Object result = new Object();
134 result = "null".equals(jsonObj.getString(fieldName)) ? "" : jsonObj.getString(fieldName);
137 result = jsonObj.getInt(fieldName);
140 result = jsonObj.getJSONArray(fieldName);
143 result = jsonObj.getJSONObject(fieldName);
146 result = jsonObj.getLong(fieldName);
154 } catch(JSONException e) {
155 LOG.error("function=getJsonFieldLong, exception: {} ", e);
163 * Check whether the Json Object is empty.<br>
169 public static boolean isNullJson(JSONObject jsonObject) {
170 if(null == jsonObject || jsonObject.isEmpty()) {
178 * Get String value by Json.<br>
185 public static String getStrValueByjson(JSONObject json, String key) {
186 JSONArray names = json.names();
187 String result = null;
188 for(int i = 0; i < names.size(); i++) {
189 String nodeName = names.getString(i);
190 if(json.optJSONObject(nodeName) != null) {
191 result = getStrValueByjson(json.getJSONObject(nodeName), key);
193 if(json.optJSONArray(nodeName) != null) {
194 result = getStrValueByJArray(json.getJSONArray(nodeName), key);
196 if(nodeName.equals(key)) {
197 result = json.getString(nodeName);
204 private static String getStrValueByJArray(JSONArray json, String key) {
205 String result = null;
206 for(int i = 0; i < json.size(); i++) {
207 if(json.optJSONObject(i) != null) {
208 result = getStrValueByjson(json.getJSONObject(i), key);
210 if(json.optJSONArray(i) != null) {
211 result = getStrValueByJArray(json.getJSONArray(i), key);
219 * Get Json Value by Json object.<br>
226 public static JSONObject getJsonValueByjson(JSONObject json, String key) {
227 JSONObject resultJson = new JSONObject();
228 String result = getStrValueByjson(json, key);
232 resultJson.element(key, result);
239 * Get String Value by Json object.<br>
247 public static String getStrValueByJson(JSONObject json, String parentKey, String key) {
248 if(parentKey.isEmpty()) {
249 return getStrValueByjson(json, key);
251 JSONObject parentJson = getJsonValueByjson(json, parentKey);
252 if(isNullJson(parentJson)) {
255 return getStrValueByjson(parentJson, key);
261 * Get response Data.<br>
267 public static JSONObject getResponseData(JSONObject obj) {
268 JSONObject result = new JSONObject();
270 Integer retCode = getJsonFieldInt(obj, ParamConstant.PARAM_RETCODE);
271 if(null == retCode || retCode == -1) {
275 if(obj.optJSONObject(ParamConstant.PARAM_DATA) != null) {
276 result = obj.getJSONObject(ParamConstant.PARAM_DATA);
277 } else if(obj.optJSONArray(ParamConstant.PARAM_DATA) != null) {
278 result = obj.getJSONArray(ParamConstant.PARAM_DATA).getJSONObject(0);
280 if(result.isEmpty()) {
284 if(result.containsKey(ParamConstant.PARAM_RETCODE)) {
285 result = getResponseData(result);