首页 \ 问答 \ 使用Gson如何将变量名称添加为REST调用的json的顶级参数(Using Gson how to add variable name as top level parameter for json of a REST call)

使用Gson如何将变量名称添加为REST调用的json的顶级参数(Using Gson how to add variable name as top level parameter for json of a REST call)

我正在尝试调用REST服务并使用gson我得到以下java pojo的以下json。

POJO

public class AlphaParameters {
private  int one;
private int two;
private int three;

//getter setters
//constructors
}

JSON

{"one":4,
"two":5,
"three":10
}

我使用以下代码

Gson gson = new Gson()
AlphaParameters alphaParameters = new AlphaParameters(one,two,three);
gson.toJson(alphaParameters );

之前这段代码曾经工作过,但现在看来.net上的服务器端改变了它们的实现,现在他们期望以下格式的json。 一切都是一样的,但现在看来他们想要在json中使用顶级变量名。

{"alphaParameters":
    {"one":4,
    "two":5,
    "three":10
    }
}

问题 :是否有一个特定的Gson API,我可以使用它来生成上面的json而不重构我的代码? 或者编写包装类以包含alphaParameters将是一种更好的方法。 (我将不得不为后者编写很多样板代码)。

谢谢你的帮助。


I am trying call a REST service and using gson I am getting the following json for the following java pojo.

pojo

public class AlphaParameters {
private  int one;
private int two;
private int three;

//getter setters
//constructors
}

Json

{"one":4,
"two":5,
"three":10
}

I am using the following code

Gson gson = new Gson()
AlphaParameters alphaParameters = new AlphaParameters(one,two,three);
gson.toJson(alphaParameters );

Earlier this code used to work, but now seems the server side which is on .net changed their implementation and now they are expecting the json in the following format. Everything is same but seems now they want the toplevel variable name in the json.

{"alphaParameters":
    {"one":4,
    "two":5,
    "three":10
    }
}

Question : Is there a specific api of Gson which I can use to generate the above json without refactoring my code ? Or writing a wrapper class to include alphaParameters will be a better approach . ( I will have to write a lot of boilerplate code for latter ).

Thanks for your help.


原文:https://stackoverflow.com/questions/23750966
更新时间:2021-08-07 21:08

最满意答案

正如charliefl所说,这是一个CORS问题。 MailChimp不支持CORS,主要是因为它要求您将API凭据传递给网页用户,允许他们接管整个帐户。

MailChimp的两个选项是通过服务器代理您的请求,或者,为了将人员签名到您的列表,您可以构建一个使用更受限制的API 的自定义注册表单 。 第二种方法的警告是它通过MailChimp的双重选择过程强制所有订阅。


As charliefl noted, this is a CORS issue. MailChimp doesn't support CORS, mostly because it would require you passing your API credentials to the user of the webpage, allowing them to takeover your entire account.

Your two options for MailChimp are to proxy your requests through a server or, for signing people up to your list, you can build a custom signup form that uses a much more restricted API. The caveat of this second method is that it forces all of your subscribes through MailChimp's double opt-in process.

相关问答

更多
  • 您可以通过查询广告系列集合一次获得多个广告系列。 但是,您可能无法一次性检索它们,具体取决于有多少。 You can get multiple campaigns at a time by querying the campaigns collection. You may not be able to retrieve them all at once, though, depending on how many there are.
  • 您链接的页面看起来像是来自测试版的文档,但不管怎样,他们说批量操作尚未实现。 FWIW,真正的文档还列出批处理操作作为路线图的一部分,所以我怀疑它们还没有完成。 The page you're linking to look like docs from the beta, but either way, they say that batch operations aren't yet implemented. FWIW, the real docs also list Batch Operations ...
  • 您需要设置基本身份验证标头。 同样,你似乎将你的dataCentre包含在你的API密钥中,所以下面的代码可能需要一些调整,因为我实际上并不知道$mailchimp_api_key设置为什么。 // this is improper usage of isset. isset returns a boolean // since you're not doing a strict match it'll always work, // unless mailtest is null or isn't se ...
  • 我刚才在这里回答了这个问题 - mailchimp 3.0以html格式获取模板 目前还没有直接获取模板来源的方法,但您可以使用模板制作广告系列,然后获取该广告系列的来源。 http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/content/ I just answered this question over here - mailchimp 3.0 Get template in html form The ...
  • 如果通过“subscribe”表示您的应用程序将某人添加到邮件列表中,您可能需要查看其文档的List Members Collection部分。 http://kb.mailchimp.com/api/resources/lists/members/lists-members-collection If by "subscribe" you mean that your application will add someone to a mailing list, you may want to take ...
  • 正如charliefl所说,这是一个CORS问题。 MailChimp不支持CORS,主要是因为它要求您将API凭据传递给网页用户,允许他们接管整个帐户。 MailChimp的两个选项是通过服务器代理您的请求,或者,为了将人员签名到您的列表,您可以构建一个使用更受限制的API 的自定义注册表单 。 第二种方法的警告是它通过MailChimp的双重选择过程强制所有订阅。 As charliefl noted, this is a CORS issue. MailChimp doesn't support CO ...
  • 在通话double_optin设置为false 。 例: $result=$mailChimp->call("lists/subscribe", array( "id"=>"d12a719916", "email"=>array("email"=>"testuser@somedomain.com"), "double_optin"=>false, "update_existing"=>true, "send_welcome"=>false, )); 更多信息在这里 。 Set double_optin t ...
  • 几个小时后,我们终于发现了导致这种情况的原因 当RestSharp向https://us2.api.mailchimp.com/3.0/发出请求时,它选择省略尾随'/'(即使你在RestRequest中专门添加它,例如: new RestRequest("/", Method.GET) ) 所以请求是https://us2.api.mailchimp.com/3.0 这导致服务器端重定向到' https://us2.api.mailchimp.com/3.0/ '(尾随'/'),由于某种原因,此重定向清除了 ...
  • 当您通过AJAX请求传递序列化数据时, if(isset($_POST['submit']))将不会评估为true,因为该按钮不是通过请求发送的数据的一部分。 您可以使用if($_SERVER['REQUEST_METHOD'] == "POST")来检查请求是否已提交。 只需在PHP文件中更新它,它应该工作! When you're passing serialized data through the AJAX request, if(isset($_POST['submit'])) won't eva ...
  • 您正在查看测试版中严重过时的文档 - 您最好查看API Playground ,但您要查找的端点是interest-categories 。 You're looking at severely outdated docs from the beta -- you'd be better off checking out the API Playground, but the endpoint you're looking for is interest-categories.

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)