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)
上一篇 2022年5月15日 下午5:00
下一篇 2022年5月15日 下午5:00


相关推荐

  • 再议公交查询算法

    再议公交查询算法

    2021年7月23日
    70
  • c语言中字符串比较的库函数是什么_c语言比较字符串大小

    c语言中字符串比较的库函数是什么_c语言比较字符串大小在单片机串口实现字符串命令解析这篇文章中分析了在串口通信中如何去解析字符串命令,这篇文章就来讨论下字符串比较的方法都有哪些?说起比较运算,肯定第一时间想到了C语言中关于比较的相关运算符“>、<、!=、>=、<=、==”,那么要比较两个字符串是否相等是不是直接用“==”比较就行了。下面就来看看这种方法行不行?先看一个例子voidmain(void){chars1[]=”abc”;chars2[]…

    2025年7月24日
    7
  • mptcp的重传

    mptcp的重传开头啰嗦几句 关于 mptcp 的研究主要有两种 拥塞控制和数据包调度 关于多径的拥塞控制算法有 LIA OLIA BALIA wVegas 可以称作是耦合式的拥塞控制 为什么要采用耦合式的拥塞控制 而不是在各个子流上采用原有的 TCP 的拥塞控制算法 RENO CUBIC 呢 用学术点的话说 考虑到网络中大部分是 TCP 流 MPTCP 应该保有 TCP 友好的特性 用朴实的话说 做为一个研究项目 总要有些不同吧

    2026年1月30日
    3
  • django配置文件详解_django 日志配置和使用

    django配置文件详解_django 日志配置和使用前言Django的配置文件settings.py用于配置整个网站的环境和功能,核心配置必须有项目路径、密钥配置、域名访问权限、App列表、中间件、资源文件、模板配置、数据库的连接方式基本配置信息

    2022年7月31日
    8
  • 阿里巴巴主流数据库连接池Druid入门

    阿里巴巴主流数据库连接池Druid入门一.数据库连接池的必要性(一).传统数据库连接模式的的步骤1.在主程序中创建连接2.进行sql操作3.关闭数据库连接(二).传统数据库连接模式存在的问题1.浪费时间:每次连接时都要验证登录和将conn加载到内存,2.不能大规模的访问数据库:当数据库访问人数过多时,占用大量系统资源,会导致服务器崩溃3.存在内存泄漏问题:每次连接都需要断开连接,如果不断开,程序运行结束,会有创建的连接对象存在内存中一直无法关闭,就会导致java内存…

    2022年7月23日
    14
  • matlab secant method

    matlab secant method

    2021年12月16日
    53

发表回复

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

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