java怎么将string转为json对象 string可以转换为json吗
java怎么将string转为json对象
引入jar包json-lib-2.4JSONObjectjsonObject=JSONObject.fromObject("json格式的字符串")StringjsonStr="{id:2}"JSONObjectjsonObject=JSONObject.fromObject(jsonStr)intid=jsonObject.getInt("id")
string可以转换为json吗
1、string转json
有三种方法
第一种:string直接转json
String json = "{"2":"efg","1":"abc"}" JSONObject json_test = JSONObject.fromObject(json) 将string的双引号转义即可,适用于字符串较短的
第二种:将string转为list后转为json
List
可以使用list的add函数将需要的字符串拼接即可,但是这个只能使用jsonarry
java怎么把字符串转成json对象
下面以ali的fastjson为例,讲解json字符串转json对象:String jsonStr = "{"message" : "success"}"JSONObject jsonObject = JSON.parseObject(jsonStr)String message = jsonObject.getString("message")System.out.println(message)
如何把String转化为JSON Object
你试试这个private static String convertStreamToString(InputStream is) {BufferedReader reader = new BufferedReader( new InputStreamReader(is))StringBuilder sb = new StringBuilder()String line = nulltry { while ((line = reader.readLine()) != null) { sb.append(line " ") }} catch (IOException e) { // Log.e(TAG, e.getMessage(), e) throw new RuntimeException(e.getMessage(), e)} finally { try { is.close() } catch (IOException e) { // Log.e(TAG, e.getMessage(), e) throw new RuntimeException(e.getMessage(), e) }}return sb.toString()}
java中object数据怎么转换成json数据
你可以通过这个(json-lib-2.3-jdk15.jar)jar里的方法转换
JSONObject json = JSONObject.fromObject(Object)
如果对象数组
JSONArray json = JSONArray .fromObject(person)