Map<String,Object>转为JSONObject ,JSONObject 转为JsonObject,JsonObject转为JsonElement

Map<String,Object>转为JSONObject ,JSONObject 转为JsonObject,JsonObject转为JsonElementpackagecom.tianjian.property.jieshuns.service;importjava.util.Map;importjava.util.Properties;importorg.apache.http.HttpStatus;importorg.apache.http.client.methods.CloseableHttpRes

大家好,又见面了,我是你们的朋友全栈君。package com.tianjian.property.jieshuns.service;

import java.util.Map;

import java.util.Properties;

import org.apache.http.HttpStatus;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.util.EntityUtils;

import com.google.gson.JsonArray;

import com.google.gson.JsonElement;

import com.google.gson.JsonObject;

import com.google.gson.JsonParser;

import com.tianjian.property.jieshuns.utils.CommonServices;

import com.tianjian.property.jieshuns.utils.ConfigHelpers;

import com.tianjian.property.jieshuns.utils.Utils;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

public class SaveorUpdateServices extends CommonServices {


@Override
public String buildRequestParam(Map<String,Object> params) {

System.out.println(“——params66——“+params.get(“jsonusers”));

ConfigHelpers   con=new ConfigHelpers();
         Map<String,Object> map=con.getproper(“/config/”+baseDir+”saveorupdate”);
         Properties prop= ConfigHelpers.getPropertiess(map);
JsonObject jsonParam = new JsonObject();
JsonObject attributes = new JsonObject();

// jsonParam
attributes.addProperty(“serviceId”, prop.getProperty(“serviceId”));//JsonObject对象addProperty第二个参数是普通的String。
attributes.addProperty(“requestType”, prop.getProperty(“requestType”));

System.out.println(“——params——“+params.get(“jsonusers”));

               //Map<String,Object>型参数转为JSONObject

net.sf.json.JSONObject  attribute012;
attribute012= (JSONObject) params.get(“jsonusers”);
System.out.println(“——attribute012——“+attribute012);
  String attribute013= attribute012.toString();

//JSONArray attribute123;
//attribute123=attribute012.getJSONArray(“jsonusers”);
// System.out.println(“——attribute123——“+attribute123);
//JsonArray attribute;
//attribute=   (JsonArray) params.get(“jsonusers”);
//System.out.println(“——attributes001——“+attribute);
//JsonElement jsonelement = null;
String attributes01= attribute012.get(“jsonusers”).toString();
System.out.println(“——attributes01——“+attributes01);

String attributes02=attributes01.replaceAll(“\\\\”, “”);

System.out.println(“——attributes02——“+attributes02);

//JSONObject转为JsonObject ,通过先转成对应的String然后转为JsonObject 

JsonObject json=new JsonParser().parse(attribute013).getAsJsonObject();
System.out.println(“–555—-json——“+json);

JsonElement dataItems=json.get(“jsonusers”);
System.out.println(“–555—-dataItems——“+dataItems);

JsonElement value = null;

attributes.add(“attributes”, dataItems);//JsonObject对象add第二个参数是JsonElement的对象。

jsonParam.add(“attributes”, attributes);
System.out.println(“—facebook——jsonParam:”+jsonParam);
System.out.println(“—facebook——attributes:”+attributes);
//return jsonParam.toString();
return attributes.toString();
}


@Override


public String extractResult(CloseableHttpResponse response)


throws Exception {


int statusCode = response.getStatusLine().getStatusCode();


String dataItem=””;


System.out.println(“0666666″+statusCode);


if (statusCode == HttpStatus.SC_OK) {


String results = EntityUtils.toString(response.getEntity());


JsonObject json = new JsonParser().parse(results).getAsJsonObject();


int resultCode = json.get(“resultCode”).getAsInt();


System.out.println(“1666666″+json);


System.out.println(“2666666″+resultCode);


if (resultCode == 0) {


JsonElement dataItems = json.get(“dataItems”);


System.out.println(“666666″+dataItems);


JsonArray jsonarray=json.getAsJsonArray(“dataItems”);


JsonObject dds=(JsonObject) jsonarray.get(0);


JsonObject attributes=dds.getAsJsonObject(“attributes”);


dataItem=attributes.toString();


System.out.println(“ReplacePayFee-SUCCESS:成功!同步成功信息:” + dataItems.toString());





} else {


System.out.println(“ReplacePayFee-ERROR:执行失败!” + “\tstatusCode:” + statusCode);


}


return dataItem;


}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/146220.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 微信小程序js 字符串截取[通俗易懂]

    微信小程序js 字符串截取[通俗易懂]微信小程序js字符串截取console.log(time)输出:qwertyuiopconsole.log(time.substring(0,5))输出:qwert

    2022年6月4日
    188
  • datax实现mysql数据同步

    datax实现mysql数据同步datax同步数据使用详解

    2022年5月17日
    80
  • Android物联网应用程序开发(智慧城市)—— 火焰监控界面开发

    Android物联网应用程序开发(智慧城市)—— 火焰监控界面开发效果:布局代码:<?xmlversion=”1.0″encoding=”utf-8″?><RelativeLayoutxmlns:android=”http://schemas.android.com/apk/res/android”xmlns:app=”http://schemas.android.com/apk/res-auto”xmlns:tools=”http://schemas.android.com/tools”androi.

    2022年6月21日
    34
  • 安卓activity生命周期_请描述activity的生命周期

    安卓activity生命周期_请描述activity的生命周期Activity作为Android的四大组件之一,非常的重要,也是最常用的四大组件,使用Activity必须要在AndroidManifest中进行注册,那么作为Android的基础,Activity的生命周期你是否完全掌握了呢?下面就让我们来一起回顾一下Activity的生命周期吧!首先,你需要知道Activity的生命周期分为两种。 典型情况下的生命周期(就是我们开发中经常用到的…

    2022年8月16日
    5
  • 计算机二级C语言公共基础知识,以及习题总结(一)

    计算机二级C语言公共基础知识,以及习题总结(一)二级C语言公共基础知识,以及习题总结,算法的基本概念,算法的复杂度,数据结构的基本概念,什么是数据结构,数据结构的图形表示,线性结构和非线性结构,线性表的基本概念,线性表的顺序存储结构,栈和队列,队列及其基本运算,线性链表的基本概念

    2022年5月27日
    35
  • C/C++编程学习 – 第5周 ④ 石头剪刀布「建议收藏」

    C/C++编程学习 – 第5周 ④ 石头剪刀布「建议收藏」题目描述石头剪刀布是常见的猜拳游戏。石头胜剪刀,剪刀胜布,布胜石头。如果两个人出拳一样,则不分胜负。一天,小A和小B正好在玩石头剪刀布。已知他们的出拳都是有周期性规律的,比如:“石头-布-石头-剪刀-石头-布-石头-剪刀……”,就是以“石头-布-石头-剪刀”为周期不断循环的。请问,小A和小B比了N轮之后,谁赢的轮数多?输入格式输入包含三行。第一行包含三个整数:N,NA,NB,分别表示比了N轮,小A出拳的周期长度,小B出拳的周期长度。0

    2022年7月24日
    18

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号