jsonobject

Java中List集合和JSON对象之间的相互转换

吃可爱长大的小学妹 提交于 2019-12-26 00:32:33
第一种方法: 代码实现 /** *数据封装成json * * @param items 物料入库数据 * @return json * @throws JSONException */ public static String GoodIn2Json(List<GoodInfo> items) throws JSONException { if (items == null) return ""; JSONArray array = new JSONArray(); JSONObject jsonObject = null; GoodInfo info = null; for (int i = 0; i < items.size(); i++) { info = items.get(i); jsonObject = new JSONObject(); jsonObject.put(Api.COLORID, info.getColorId()); jsonObject.put(Api.STOCK, info.getStock()); array.put(jsonObject); } return array.toString(); } /** * 将json数组解析出来,生成自定义数据的数组 * @param data 包含用户自定义数据的json * @return

百度大脑之通用文字识别进行图像修复攻略

会有一股神秘感。 提交于 2019-12-25 20:44:51
【通用文字识别另类使用】进行图像修复 作者:756665228 OCR的另类使用。你相信OCR可以进行图片修复吗?不管信不信。反正小帅是这样做了一个案例。 接下来就紧跟步伐看小帅是如何实现这样的功能吧 实现步骤 Step1:成为百度AI开放平台的开发者 我们有账号之后登录,并且点击此处(文字识别)创建一个应用,如下图 然后就能看到创建完的应用和 APPID、API KEY 以及 Secret KEY了 由于图像修复是另一个模块下的接口,为了不创建过多的应用。咱们开发者需要记住APPID。申请加入百度图像识别官方QQ群(群号:659268104),提供公司名称、APPID、应用场景,等待百度工作人员协助开通权限后方可使用。 Step2:准备数据 文字识别服务可以让小帅把自己拍摄的含有文字的图片转化成文本数据,然后就可以对文字进行编辑等操作,咱们需要借助于OCR部分数据进行图片修复。那小帅提前准备的图片如下 Step3: 编写一个文字识别示例程序 有 第一步 的 API KEY 以及 Secret KEY,以及 第二步 的数据,我们就可以写一个示例代码调用百度AI开放平台的文字识别能力 准备开发环境 小帅选择用 Java来快速搭建一个原型,关于如何安装Java。可以参考百度经验哦~。百度AI有很完善的API文档、和封装调用更方便的工具包。接下来小帅就用Maven搭建工程环境 pom

Json array inside array retrieve values Android

跟風遠走 提交于 2019-12-25 18:43:29
问题 i m trying to get values from JSONArray inside array, i m able to retrieve whole JSON values into JSONArray successfully but not able to retrieve values inside JSONArray . When i convert JSONArray to JSONObject to get values stored inside JSONArray . It gives error: org.json.JSONException: No value for "banner" Here is JSON code, i verified JSON code with jsonlint.com and it showed JSON is Validate, [ {"code":"banner","moduletitle":0, "banner": [ {"image":"http://imageurl"}, {"image":"http:/

$Java-json系列(二):用JSONObject解析和处理json数据

时光总嘲笑我的痴心妄想 提交于 2019-12-25 16:34:36
本文转载自: https://www.cnblogs.com/jiayongji/p/6417862.html 作者:jiayongji 转载请注明该声明。 本文中主要介绍 JSONObject 处理json数据时候的一些常用场景和方法。 (一)jar包下载 所需jar包打包下载百度网盘地址: https://pan.baidu.com/s/1c27Uyre (二)常见场景及处理方法 1、解析简单的json字符串: 1      // 简单的json测试字符串 2 public static final String JSON_SIMPLE = "{'name':'tom','age':16}" ; 3 4 JSONObject obj = JSONObject.fromObject(JSON_SIMPLE); 5 System.out.println("name is : " + obj.get("name" )); 6 System.out.println("age is : " + obj.get("age")); 输出: name is : tom age is : 16 2、解析嵌套的json字符串: 1      // 嵌套的json字符串 2 public static final String JSON_MULTI = "{'name':'tom','score':

Null Pointer exception on JSON.getString JSONObject

笑着哭i 提交于 2019-12-25 16:09:33
问题 I am trying to get the username from a database using PHP on android. I have it set up on my localhost. the output on localhost is: [{"id":"1","username":"Josh","password":"pass"}] The PHP is: <?php $con = $con = mysql_connect("localhost", "root", "root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("natter", $con); $result = mysql_query("SELECT * FROM users"); while($row = mysql_fetch_assoc($result)) { $output[] = $row; } print(json_encode($output)); mysql

Null Pointer exception on JSON.getString JSONObject

爷,独闯天下 提交于 2019-12-25 16:09:00
问题 I am trying to get the username from a database using PHP on android. I have it set up on my localhost. the output on localhost is: [{"id":"1","username":"Josh","password":"pass"}] The PHP is: <?php $con = $con = mysql_connect("localhost", "root", "root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("natter", $con); $result = mysql_query("SELECT * FROM users"); while($row = mysql_fetch_assoc($result)) { $output[] = $row; } print(json_encode($output)); mysql

微信二次分享失败解决

无人久伴 提交于 2019-12-25 10:47:04
前段时间写了一片微信分享的文章,在二次分享时会存在问题,今天进行补充,主要是第五条 1、通过appId和secret获取access_token public static String getAccessToken(String appid, String secret) { String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret; JSONObject jsonObject = httpRequest(url, "GET", null); RTLogger.getLogger("WechatLog").debug("重新获取微信接口token返回:"+jsonObject); try { if(jsonObject.getString("errcode")!=null){ RTLogger.getLogger("WechatLog").debug("重新获取微信接口errcode返回:"+jsonObject.getString("errcode")); return "false"; } }catch (Exception e) { } return jsonObject.getString(

Convert a JSON object to another JSON object in Java

做~自己de王妃 提交于 2019-12-25 09:32:45
问题 Java class JSONObject : I have a JSON object, from which I want to extract only some of the key/value pairs into another JSON object. In addition, I want to change the names of the extracted keys. And finally, I want to have the output object "flat" (i.e., all the elements in depth 1). For example: Input object: { "a":"val_a", "b": { "b1":"val_b1", "b2":"val_b2" }, "c": { "c1":"val_c1", "c2": { "c21":"val_c21", "c22":"val_c22" } } } Output object: { "x":"val_a", "y":"val_b1", "z":"val_c22" }

How to convert a json data to string in java

喜夏-厌秋 提交于 2019-12-25 05:20:16
问题 I want to convert json data to string import java.io.BufferedReader; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONArray; import org.json.JSONObject; public static void main(String[] args) throws Exception { URL url = new URL("http://192.168.1.13/test/ProductWb.php?productId=9"); HttpURLConnection conn ; conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn

java代码备份

萝らか妹 提交于 2019-12-24 22:24:37
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 从api获取基站信息 public static JSONArray getMncInfo(JSONObject latLngJo, String operatorCode, String mncKey, CellRepository cellRepository) { if (!operatorCode.equals("0") && !operatorCode.equals("1")) { operatorCode = "11"; } HttpRes httpRes = new HttpRes(); JSONObject latLngJoNew; JSONArray mncInfoJaOld = new JSONArray(), mncInfoJa = new JSONArray(); // api服务停止,暂时从数据库随机获取 /*List<CellInfo> cellInfoList = cellRepository.findCellInfo(r.nextInt(14)); for (int i=0; i<cellInfoList.size(); i++) { CellInfo cellInfo = cellInfoList.get(i); JSONObject mncInfoJo = new