Refine search
public void actionPerformed(ActionEvent e) { String json = editTP.getText(); json = json.trim(); if (json.startsWith("{")) { JSONObject jsonObject = new JSONObject(json); String formatJson = jsonObject.toString(4); editTP.setText(formatJson); } else if (json.startsWith("[")) { JSONArray jsonArray = new JSONArray(json); String formatJson = jsonArray.toString(4); editTP.setText(formatJson); } } });
public String listmap_to_json_string(List<Map<String, Object>> list) { JSONArray json_arr=new JSONArray(); for (Map<String, Object> map : list) { JSONObject json_obj=new JSONObject(); for (Map.Entry<String, Object> entry : map.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); try { json_obj.put(key,value); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } json_arr.put(json_obj); } return json_arr.toString(); }
@Override public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) { try { JSONArray object = new JSONObject(s).getJSONObject("showapi_res_body").getJSONObject("pagebean").getJSONArray("contentlist"); Type newsContentType = new TypeToken<List<NewsContent>>() {}.getType(); if (isMore) { List<NewsContent> more = new Gson().fromJson(object.toString(), newsContentType); newsContentList.addAll(0, more); } else { newsContentList = new Gson().fromJson(object.toString(), newsContentType); } mAdapter.setData(newsContentList); page++; ptr.refreshComplete(); } catch (JSONException e) { e.printStackTrace(); } } });
@Override public <T> void addPolyline(AirMapPolyline<T> polyline) { try { JSONArray array = new JSONArray(); for (LatLng point : polyline.getPoints()) { JSONObject json = new JSONObject(); json.put("lat", point.latitude); json.put("lng", point.longitude); array.put(json); } webView.loadUrl(String.format( "javascript:addPolyline(" + array.toString() + ", %1$d, %2$d, %3$d);", polyline.getId(), polyline.getStrokeWidth(), polyline.getStrokeColor())); } catch (JSONException e) { Log.e(TAG, "error constructing polyline JSON", e); } }
/** * json 格式化 * * @param json * @return */ public static String jsonFormat(String json) { if (TextUtils.isEmpty(json)) { return "Empty/Null json content"; } String message; try { json = json.trim(); if (json.startsWith("{")) { JSONObject jsonObject = new JSONObject(json); message = jsonObject.toString(4); } else if (json.startsWith("[")) { JSONArray jsonArray = new JSONArray(json); message = jsonArray.toString(4); } else { message = json; } } catch (JSONException e) { message = json; } return message; }
@Override public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) { try { emptyView.setVisibility(View.GONE); JSONArray object = new JSONObject(s).getJSONObject("showapi_res_body").getJSONArray("channelList"); Type channelItemType = new TypeToken<List<NewsChannel>>() {}.getType(); List<NewsChannel> channelItems = new Gson().fromJson(object.toString(), channelItemType); viewPager.setAdapter(new ChannelAdapter(getSupportFragmentManager(), channelItems)); tab.setViewPager(viewPager); } catch (JSONException e) { e.printStackTrace(); } } });
private void handlePageList(LightHttpResponse response) throws JSONException { if (mPageListResponse == null) { JSONArray reply = new JSONArray(); JSONObject page = new JSONObject(); page.put("type", "app"); page.put("title", makeTitle()); page.put("id", PAGE_ID); page.put("description", ""); page.put("webSocketDebuggerUrl", "ws://" + mInspectorPath); Uri chromeFrontendUrl = new Uri.Builder() .scheme("http") .authority("chrome-devtools-frontend.appspot.com") .appendEncodedPath("serve_rev") .appendEncodedPath(WEBKIT_REV) .appendEncodedPath("devtools.html") .appendQueryParameter("ws", mInspectorPath) .build(); page.put("devtoolsFrontendUrl", chromeFrontendUrl.toString()); reply.put(page); mPageListResponse = LightHttpBody.create(reply.toString(), "application/json"); } setSuccessfulResponse(response, mPageListResponse); }
@Override public String format(String json) { String formattedString = null; if (json == null || json.trim().length() == 0) { throw new FormatException("JSON empty."); } try { if (json.startsWith("{")) { JSONObject jsonObject = new JSONObject(json); formattedString = jsonObject.toString(JSON_INDENT); } else if (json.startsWith("[")) { JSONArray jsonArray = new JSONArray(json); formattedString = jsonArray.toString(JSON_INDENT); } else { throw new FormatException("JSON should start with { or [, but found " + json); } } catch (Exception e) { throw new FormatException("Parse JSON error. JSON string:" + json, e); } return formattedString; } }
public static List<LiveBean> getLiveList() { List<LiveBean> list = new ArrayList<>(); OkHttpClient client = new OkHttpClient .Builder() .connectTimeout(30, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://apikg.kktv1.com:8080") .client(client) .addConverterFactory(ScalarsConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); ApiService mApiServicePresenter = retrofit.create(ApiService.class); Call<String> call = mApiServicePresenter.live("{\"platform\":2,\"count\":2,\"start\":0,\"c\":90013,\"FuncTag\":80010001,\"a\":10}"); Response<String> response = null; try { response = call.execute(); String body = response.body(); JSONObject js = new JSONObject(body); if (body != null) { List<LiveBean> temp = JSON.parseArray(js.getJSONArray("roomList").toString(), LiveBean.class); list.addAll(temp); } } catch (Exception e) { e.printStackTrace(); } return list; }
@Override public <T> void addPolygon(AirMapPolygon<T> polygon) { try { JSONArray array = new JSONArray(); for (LatLng point : polygon.getPolygonOptions().getPoints()) { JSONObject json = new JSONObject(); json.put("lat", point.latitude); json.put("lng", point.longitude); array.put(json); } webView.loadUrl(String.format(Locale.US, "javascript:addPolygon(" + array.toString() + ", %1$d, %2$d, %3$d, %4$d);", polygon.getId(), (int) polygon.getPolygonOptions().getStrokeWidth(), polygon.getPolygonOptions().getStrokeColor(), polygon.getPolygonOptions().getFillColor())); } catch (JSONException e) { Log.e(TAG, "error constructing polyline JSON", e); } }
@Override public void json(@Nullable String json) { if (Utils.isEmpty(json)) { d("Empty/Null json content"); return; } try { json = json.trim(); if (json.startsWith("{")) { JSONObject jsonObject = new JSONObject(json); String message = jsonObject.toString(JSON_INDENT); d(message); return; } if (json.startsWith("[")) { JSONArray jsonArray = new JSONArray(json); String message = jsonArray.toString(JSON_INDENT); d(message); return; } e("Invalid Json"); } catch (JSONException e) { e("Invalid Json"); } }
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Query post = RemoteAccess.evaluate(request); // manage DoS if (post.isDoS_blackout()) {response.sendError(503, "your request frequency is too high"); return;} // evaluate get parameters String data = post.get("data", ""); try { String json = data; JSONArray array = CDL.toJSONArray(json); PrintWriter sos = response.getWriter(); sos.print(array.toString(2)); sos.println(); } catch (IOException e) { DAO.severe(e); JSONObject json = new JSONObject(true); json.put("error", "Malformed CSV. Please check CSV Again"); json.put("type", "Error"); PrintWriter sos = response.getWriter(); sos.print(json.toString(2)); sos.println(); } post.finalize(); }
private void fillServerPort() { try { URL url = new URL("http://localhost:" + _port + "/pools/default/buckets"); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestProperty("Accept", "application/json"); httpURLConnection.setRequestMethod("GET"); if (200 <= httpURLConnection.getResponseCode() && httpURLConnection.getResponseCode() <= 299) { BufferedReader br = new BufferedReader(new InputStreamReader((httpURLConnection.getInputStream()))); StringBuilder sb = new StringBuilder(); String output; while ((output = br.readLine()) != null) { sb.append(output); } JSONArray json = new JSONArray(sb.toString()); log.debug(json.toString()); int serverPort = (Integer) ((JSONObject) ((JSONObject) ((JSONArray) ((JSONObject) json.get(0)).get("nodes")).get(0)).get("ports")).get("direct"); _serverPort = serverPort; } } catch (Exception e) { log.error("Server is not up", e); Throwables.propagate(e); } }