List of usage examples for com.google.gson JsonParser parse
@Deprecated public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException
From source file:com.ccc.crest.core.client.CrestClient.java
License:Open Source License
public static String prettyPrintJson(String ugly) { Gson gson = new GsonBuilder().setPrettyPrinting().create(); JsonParser jp = new JsonParser(); JsonElement je = jp.parse(ugly); return gson.toJson(je); }
From source file:com.citrix.developer.storefront_api_android_sample.ResourceListing.java
License:Open Source License
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //R.layout.activity_resource_listing setContentView(R.layout.activity_resource_listing); Bundle bndl = this.getIntent().getExtras(); String JSONResourceList = bndl.getString("JSONRESOURCE"); //load up the listview. _appResources = new ArrayList<Resource>(); JsonParser parser = new JsonParser(); JsonElement o = parser.parse(JSONResourceList); JsonObject a = o.getAsJsonObject();//from w w w . j a va 2 s . c om JsonArray _resources = a.get("resources").getAsJsonArray(); for (JsonElement _resource : _resources) { JsonObject _obj = _resource.getAsJsonObject(); Resource _r = new Resource(); _r.AppTitle = _obj.get("name").getAsString(); _r.AppLaunchURL = _obj.get("launchurl").getAsString(); try { _r.AppDesc = _obj.get("description").getAsString(); } catch (Exception descException) { //do nothing } _r.AppIcon = _obj.get("iconurl").getAsString(); _appResources.add(_r); } ResourceAdapter _ra = new ResourceAdapter(this, _appResources); ListView _lv = (ListView) findViewById(R.id.resourceList); _lv.setAdapter(_ra); _lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { //get the launch URL String url = _appResources.get(i).AppLaunchURL; //call the launch task and pass it the launchURL new DownloadLaunchICATask(getApplicationContext(), ResourceListing.this).execute(url); } }); }
From source file:com.cloopen.rest.sdk.CCPRestSDK.java
License:Open Source License
private HashMap<String, Object> jsonToMap(String result) { HashMap<String, Object> hashMap = new HashMap<String, Object>(); JsonParser parser = new JsonParser(); JsonObject asJsonObject = parser.parse(result).getAsJsonObject(); Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet(); HashMap<String, Object> hashMap2 = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> m : entrySet) { if ("statusCode".equals(m.getKey()) || "statusMsg".equals(m.getKey())) hashMap.put(m.getKey(), m.getValue().getAsString()); else {// w w w . j a v a 2s . c o m if ("SubAccount".equals(m.getKey()) || "totalCount".equals(m.getKey()) || "smsTemplateList".equals(m.getKey()) || "token".equals(m.getKey()) || "callSid".equals(m.getKey()) || "state".equals(m.getKey()) || "downUrl".equals(m.getKey())) { if (!"SubAccount".equals(m.getKey()) && !"smsTemplateList".equals(m.getKey())) hashMap2.put(m.getKey(), m.getValue().getAsString()); else { try { if ((m.getValue().toString().trim().length() <= 2) && !m.getValue().toString().contains("[")) { hashMap2.put(m.getKey(), m.getValue().getAsString()); hashMap.put("data", hashMap2); break; } if (m.getValue().toString().contains("[]")) { hashMap2.put(m.getKey(), new JsonArray()); hashMap.put("data", hashMap2); continue; } JsonArray asJsonArray = parser.parse(m.getValue().toString()).getAsJsonArray(); ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>(); for (JsonElement j : asJsonArray) { Set<Entry<String, JsonElement>> entrySet2 = j.getAsJsonObject().entrySet(); HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> m2 : entrySet2) { hashMap3.put(m2.getKey(), m2.getValue().getAsString()); } arrayList.add(hashMap3); } hashMap2.put(m.getKey(), arrayList); } catch (Exception e) { JsonObject asJsonObject2 = parser.parse(m.getValue().toString()).getAsJsonObject(); Set<Entry<String, JsonElement>> entrySet2 = asJsonObject2.entrySet(); HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> m2 : entrySet2) { hashMap3.put(m2.getKey(), m2.getValue().getAsString()); } hashMap2.put(m.getKey(), hashMap3); hashMap.put("data", hashMap2); } } hashMap.put("data", hashMap2); } else { JsonObject asJsonObject2 = parser.parse(m.getValue().toString()).getAsJsonObject(); Set<Entry<String, JsonElement>> entrySet2 = asJsonObject2.entrySet(); HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> m2 : entrySet2) { hashMap3.put(m2.getKey(), m2.getValue().getAsString()); } if (hashMap3.size() != 0) { hashMap2.put(m.getKey(), hashMap3); } else { hashMap2.put(m.getKey(), m.getValue().getAsString()); } hashMap.put("data", hashMap2); } } } return hashMap; }
From source file:com.cloud.gate.util.JsonAccessorTestCase.java
License:Apache License
public void testJsonAccessor() { JsonParser parser = new JsonParser(); JsonElement json = parser .parse("{firstName: 'Kelven', lastName: 'Yang', arrayObj: [{name: 'elem1'}, {name: 'elem2'}]}"); JsonAccessor jsonAccessor = new JsonAccessor(json); Assert.assertTrue("Kelven".equals(jsonAccessor.getAsString("firstName"))); Assert.assertTrue("Kelven".equals(jsonAccessor.getAsString("this.firstName"))); Assert.assertTrue("Yang".equals(jsonAccessor.getAsString("lastName"))); Assert.assertTrue("Yang".equals(jsonAccessor.getAsString("this.lastName"))); Assert.assertTrue("elem1".equals(jsonAccessor.getAsString("arrayObj[0].name"))); Assert.assertTrue("elem2".equals(jsonAccessor.getAsString("arrayObj[1].name"))); Assert.assertTrue("elem1".equals(jsonAccessor.getAsString("this.arrayObj.this[0].name"))); Assert.assertTrue("elem2".equals(jsonAccessor.getAsString("this.arrayObj.this[1].name"))); Assert.assertTrue(jsonAccessor.getMatchCount("firstName") == 1); Assert.assertTrue(jsonAccessor.getMatchCount("middleName") == -1); Assert.assertTrue(jsonAccessor.getMatchCount("arrayObj") == 2); Assert.assertTrue(jsonAccessor.getMatchCount("arrayObj[0]") == 1); }
From source file:com.cloud.gate.util.JsonAccessorTestCase.java
License:Apache License
public void testGson() { String response = "{ \"queryasyncjobresultresponse\" : {\"jobid\":5868,\"jobstatus\":1,\"jobprocstatus\":0,\"jobresultcode\":0,\"jobresulttype\":\"object\",\"jobresult\":{\"snapshot\":{\"id\":3161,\"account\":\"admin\",\"domainid\":1,\"domain\":\"ROOT\",\"snapshottype\":\"MANUAL\",\"volumeid\":186928,\"volumename\":\"KY-DATA-VOL\",\"volumetype\":\"DATADISK\",\"created\":\"2011-06-02T05:05:41-0700\",\"name\":\"i-2-246446-VM_KY-DATA-VOL_20110602120541\",\"intervaltype\":\"MANUAL\",\"state\":\"BackedUp\"}}}}"; JsonParser parser = new JsonParser(); JsonElement json = parser.parse(response); JsonAccessor jsonAccessor = new JsonAccessor(json); Gson gson = new Gson(); CloudStackSnapshot snapshot = gson.fromJson( jsonAccessor.eval("queryasyncjobresultresponse.jobresult.snapshot"), CloudStackSnapshot.class); Assert.assertTrue("BackedUp".equals(snapshot.getState())); }
From source file:com.cloud.stack.CloudStackClient.java
License:Apache License
public JsonAccessor execute(CloudStackCommand cmd, String apiKey, String secretKey) throws Exception { JsonParser parser = new JsonParser(); URL url = new URL(_serviceUrl + cmd.signCommand(apiKey, secretKey)); if (logger.isDebugEnabled()) logger.debug("Cloud API call + [" + url.toString() + "]"); URLConnection connect = url.openConnection(); int statusCode; statusCode = ((HttpURLConnection) connect).getResponseCode(); if (statusCode >= 400) { logger.error("Cloud API call + [" + url.toString() + "] failed with status code: " + statusCode); String errorMessage = ((HttpURLConnection) connect).getResponseMessage(); if (errorMessage == null) { errorMessage = connect.getHeaderField("X-Description"); }/*from w ww . ja v a2 s .c om*/ if (errorMessage == null) { errorMessage = "CloudStack API call HTTP response error, HTTP status code: " + statusCode; } throw new IOException(errorMessage); } InputStream inputStream = connect.getInputStream(); JsonElement jsonElement = parser.parse(new InputStreamReader(inputStream)); if (jsonElement == null) { logger.error( "Cloud API call + [" + url.toString() + "] failed: unable to parse expected JSON response"); throw new IOException("CloudStack API call error : invalid JSON response"); } if (logger.isDebugEnabled()) logger.debug("Cloud API call + [" + url.toString() + "] returned: " + jsonElement.toString()); return new JsonAccessor(jsonElement); }
From source file:com.cloudbees.gasp.activity.GaspRESTLoaderActivity.java
License:Apache License
@Override public void onLoadFinished(Loader<RESTLoader.RESTResponse> loader, RESTLoader.RESTResponse data) { int code = data.getCode(); String json = data.getData(); // Check to see if we got an HTTP 200 code and have some data. if (code == 200 && !json.equals("")) { Log.i(TAG, "RESTLoader returns:" + json); JsonParser parser = new JsonParser(); JsonArray array = parser.parse(json).getAsJsonArray(); Iterator<JsonElement> reviews = array.iterator(); mAdapter.clear();/*from w ww . jav a 2 s. c o m*/ while (reviews.hasNext()) { mAdapter.add(reviews.next().toString()); } } else { Toast.makeText(this, getResources().getString(R.string.gasp_network_error), Toast.LENGTH_SHORT).show(); } }
From source file:com.cloudbees.gasp.fragment.GaspReviewsResponderFragment.java
License:Apache License
@Override public void onRESTResult(int code, String result) { // Here is where we handle our REST response. This is similar to the // LoaderCallbacks<D>.onLoadFinished() call from the previous tutorial. // Check to see if we got an HTTP 200 code and have some data. if (code == 200 && result != null) { JsonParser parser = new JsonParser(); JsonArray array = parser.parse(result).getAsJsonArray(); Iterator<JsonElement> reviews = array.iterator(); mList.clear();//from w ww . j av a 2 s.c o m while (reviews.hasNext()) { String theReview = reviews.next().toString(); mList.add(theReview); Log.d(TAG, "Review: " + theReview); } setReviews(); } else { Activity activity = getActivity(); if (activity != null) { Toast.makeText(activity, getResources().getString(R.string.gasp_network_error), Toast.LENGTH_SHORT) .show(); } } }
From source file:com.cn.bean.OrderInfo.java
public void setProductLineStructJson(String productLineStructJson) { // System.out.println(productLineStructJson); JsonParser parser = new JsonParser(); JsonElement element = parser.parse(productLineStructJson); JsonObject object = element.getAsJsonObject(); ProductLineStruct struct = new ProductLineStruct(); struct.setFieldName(object.get("fieldName").getAsString()); struct.setFieldValue(object.get("fieldValue").getAsString()); struct.setChildRowNum(object.get("childRowNum").getAsInt()); struct.setViewType(object.get("viewType").getAsInt()); struct.setFieldType(object.get("fieldType").getAsInt()); struct.setTextHorizon(object.get("textHorizon").getAsBoolean()); struct.setWidth(object.get("width").getAsInt()); for (int i = 0; i < struct.getChildRowNum(); i++) { String fieldName = object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject() .get("fieldName").getAsString(); //System.out.println("fieldName:" + fieldName); switch (fieldName) { case "ProductZlValue": { object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == productCommand) ? ("") : (productCommand)); break; }/* w w w . jav a2 s. c o m*/ case "ProductNameValue": { object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", ((null == productName) ? ("") : (productName)) + "/" + ((null == graphicCode) ? ("") : (graphicCode))); break; } case "ProductCodeValue": { object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == productCode) ? ("") : (productCode)); break; } case "CustomerNameValue": { object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == customerName) ? ("") : (customerName)); break; } case "PlanNumValue": { object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", planNum); break; } case "ProductGraphicValue": { object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == graphicCode) ? ("") : (graphicCode)); if (!Units.strIsEmpty(graphicCode)) object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject() .addProperty("viewType", 1); break; } case "YaZhuangOrHuaXianValue": { /* if (null != yaZhuangOrHuaXian && yaZhuangOrHuaXian.compareTo("true") == 0) yaZhuangOrHuaXian = ""; if (null != yaZhuangOrHuaXian && yaZhuangOrHuaXian.compareTo("false") == 0) yaZhuangOrHuaXian = ""; */ object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == yaZhuangOrHuaXian) ? ("") : (yaZhuangOrHuaXian)); if (!Units.strIsEmpty(yaZhuangOrHuaXian)) object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject() .addProperty("viewType", 1); break; } case "YaZhuangOrHuaXianSortValue": { /* System.out.println(yaZhuangOrHuaXianSort); if (null != yaZhuangOrHuaXianSort && yaZhuangOrHuaXianSort.compareTo("true") == 0) yaZhuangOrHuaXianSort = ""; if (null != yaZhuangOrHuaXianSort && yaZhuangOrHuaXianSort.compareTo("false") == 0) yaZhuangOrHuaXianSort = ""; */ object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == yaZhuangOrHuaXianSort) ? ("") : (yaZhuangOrHuaXianSort)); if (!Units.strIsEmpty(yaZhuangOrHuaXianSort)) object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject() .addProperty("viewType", 1); break; } case "ProductStandardValue": { object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == productStandard) ? ("") : (productStandard)); if (!Units.strIsEmpty(productStandard)) object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject() .addProperty("viewType", 1); break; } case "ProductBatchValue": { //System.out.println("productBatch:" + productBatch); object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == productBatch) ? ("") : (productBatch)); if (!Units.strIsEmpty(productBatch)) object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject() .addProperty("viewType", 1); break; } case "ProductLengthValue": { object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == productLength) ? ("") : (productLength)); if (!Units.strIsEmpty(productLength)) object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject() .addProperty("viewType", 1); break; } case "MarkColorValue": { object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == productColor) ? ("") : (productColor)); object.get("rowData" + (i + 1)).getAsJsonArray().get(2).getAsJsonObject().addProperty("fieldValue", (null == productColor1) ? ("") : (productColor1)); break; } case "MarkColorValue1": { break; } } } Gson gson = new Gson(); /* if (isGuanShu == 1) { object.addProperty("childRowNum", 7); JsonElement serialElement = parser.parse("[{\"fieldName\":\"FinishedSerial\", \"fieldValue\":\"???\", \"viewType\":1,\"fieldType\":3,\"childRowNum\":1,\"textHorizon\":true,\"width\":1}," + "{\"fieldName\":\"FinishedSerialValue\", \"fieldValue\":\"%s\", \"viewType\":1,\"fieldType\":3,\"childRowNum\":1,\"textHorizon\":true,\"width\":1}," + "{\"fieldName\":\"CurSerial\", \"fieldValue\":\"???\", \"viewType\":1,\"fieldType\":3,\"childRowNum\":1,\"textHorizon\":true,\"width\":1}," + "{\"fieldName\":\"GuanShuSerial\", \"fieldValue\":\"\", \"viewType\":2,\"fieldType\":1,\"childRowNum\":1,\"textHorizon\":true,\"width\":1}]"); object.add("rowData7", serialElement); } */ this.productLineStructJson = gson.toJson(object); // System.out.println("productLineStructJson" + this.productLineStructJson); }
From source file:com.codequicker.quick.templates.state.EngineContext.java
License:Apache License
public void setJson(String key, String payload) { if (TemplateUtil.isNullOrEmpty(payload) || TemplateUtil.isNullOrEmpty(key)) return;/* w w w . ja v a 2 s . c o m*/ JsonParser jsonParser = new JsonParser(); JsonElement jsonObject = jsonParser.parse(payload).getAsJsonObject(); context.put(key, jsonObject); }