String类型转换成json对象

2年前 (2022) 程序员胖胖胖虎阿
228 0 0

1:添加依赖:

<!-- json依赖 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.47</version>
</dependency>

2:类型转换

String count = SendRequest.sendPostJson(url, jsonObject, headerMap);
//返回查询结果
JSONObject jsonObject1 = JSONObject.parseObject(count);

字符串转json对象

JSONArray json = jsonObject1.getJSONArray("MsgList");
//获取json对象中的数组元素
Object    msgTime =  json.getJSONObject(i).get("MsgTimeStamp");
获取json对象中的普通元素
Object    jsons1 = jsons.getJSONObject(0).get("MsgContent");
//获取json数组中的对象json
 JSONObject value = (JSONObject)jsons1;
  转换成Json对象
String msgs =(String) value.get("Text");
//获取json格式中的value值

获取短信code。

{"RequestId":"eea051f1-de0a-441b-b9e3-53d2d56f8b00","SendStatusSet":[{"SerialNo":"2640:197962777016543062170869446","IsoCode":"CN","Message":"send success","Fee":1,"PhoneNumber":"+8613677494463","SessionContext":"","Code":"Ok"}]}

SendSmsResponse resp = client.SendSms(req);
// 输出json格式的字符串回包

1:转换成json

JSONObject jsonObjects = JSONObject.parseObject(count);
//转换成json

2:获取数组:

JSONArray json = jsonObjects.getJSONArray("SendStatusSet");
//获取json对象中的数组元素

3:直接获取数组中的元素:

json.getJSONObject(0).get("Code");
msg =  json.getJSONObject(0).get("Code").toString();

版权声明:程序员胖胖胖虎阿 发表于 2022年9月20日 上午5:32。
转载请注明:String类型转换成json对象 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...