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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • docker(2)CentOS 7安装docker环境[通俗易懂]

    docker(2)CentOS 7安装docker环境[通俗易懂]前言前面一篇学了mac安装docker,这篇来学习在linux上安装docker环境准备Docker支持以下的CentOS版本,目前,CentOS仅发行版本中的内核支持Docker。Doc

    2022年7月28日
    6
  • 多线程模型下的无锁编程「建议收藏」

    多线程模型下的无锁编程「建议收藏」多线程模式是比较流行的一种并发编程模型,多线程编程的一个特点就是线程间共享内存空间;这可以降低线程间通信的开销,但却引来了另外的一个难缠的问题:竟态条件!,因此,甚至有人对多线程模型提出了质疑,看这里。在多线程编程模型下,解决竟态条件的传统方法就是加锁保护临界区,但这存在影响系统性能、优先级反转等问题.因此又有人提出了,多线程模型下无锁编程的一些方式:1.线程内通信框架:Di

    2022年5月9日
    32
  • python进阶(22)pydantic–数据类型校验

    python进阶(22)pydantic–数据类型校验pydantic库的作用pydantic库是一种常用的用于数据接口schema定义与检查的库。Pydantic在运行时强制执行类型提示,并在数据无效时提供用户友好的错误信息。pydantic安

    2022年7月30日
    8
  • hostapd的分析[通俗易懂]

    hostapd的分析[通俗易懂]Hostapd的功能就是作为AP的认证服务器,负责控制管理stations(通常可以认为带无线网卡的PC)的接入和认证。通过Hostapd可以将无线网卡切换为AP/Master模式,通过修改配置文件,可以建立一个开放式的(不加密)的,WEP,WPA或WPA2的无线网络。并且通过修改配置文件可以设置无线网卡的各种参数,包括频率,信号,beacon包时间间隔,是否发送beacon包,如果响应探针请求

    2022年5月21日
    144
  • SIFT–尺度空间、高斯金字塔

    SIFT–尺度空间、高斯金字塔尺度空间高斯金字塔高斯模糊下采样高斯金字塔的构造过程差分高斯金字塔构造过程SIFT成名已久,但理解起来还是很难的,一在原作者Lowe的论文对细节提到的非常少,二在虽然网上有许多相应博文,但这些博文云里雾里,非常头疼,在查看了许多资料了,下面贴出我自己的一些理解,希望有所帮助。Lowe把SIFT分为四个阶段:构建尺度空间、关键点的定位、方向分配、特征描述符。下面分别从这四个阶段来阐述。尺度空

    2022年10月14日
    3
  • Java开发手册之并发处理

    Java开发手册之并发处理Java开发手册之并发处理

    2022年4月22日
    45

发表回复

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

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