简体   繁体   English

将Variable放在单引号中?

[英]Putting Variable with in single quotes?

Hi all I am making a request to the Campaign Monitor API and the data has to be held with in single quotes as a string but in JSON format. 大家好,我正在向Campaign Monitor API发出请求,并且数据必须以单引号作为字符串但以JSON格式保存。 As it has to be single quotes I am unable to put in any variable values. 由于必须是单引号,因此我无法输入任何变量值。 Below is the code. 下面是代码。

response = HTTParty.post(url, 
:basic_auth => auth, :body => '{
            "EmailAddress":"js@mike.com",
            "Name":"#{@fname}",
            "CustomFields":[
                  {
                            "Key":"firstName",
                            "Value":"#{@fname}"
                        },
                        {
                          "Key":"country",
                            "Value":"#{@country}"
                        }
                        ],
            "Resubscribe":true,
            "RestartSubscriptionBasedAutoresponders":true
        }')

I have tried a few different methods such as breaking the string up and piecing it back together with the variable with in double quotes but that has failed as well for the request to be successful it has to be exact. 我尝试了几种不同的方法,例如将字符串分解并将其与带双引号的变量一起拼凑回去,但是对于成功的请求来说,它也同样是失败的。

Instead of building a JSON structure by hand, you can build a Ruby hash and convert it to JSON: 您可以构建Ruby哈希并将其转换为JSON,而不是手动构建JSON结构:

require 'json'

data = {
  'EmailAddress' => 'js@mike.com',
  'Name' => @fname,
  'CustomFields' => [
    { 'Key' => 'firstName', 'Value' => @fname },
    { 'Key' => 'country', 'Value' => @country }
  ],
  'Resubscribe' => true,
  'RestartSubscriptionBasedAutoresponders' => true
}

response = HTTParty.post(url, basic_auth: auth, body: data.to_json)

Also note that there's a Ruby gem for the Campaign Monitor API: createsend-ruby 另请注意,Campaign Monitor API有一个Ruby gem: createsend-ruby

Using the gem, the above code translates to: 使用gem,以上代码可转换为:

custom_fields = [
  { 'Key' => 'firstName', 'Value' => @fname },
  { 'Key' => 'country', 'Value' => @country }
]
response = CreateSend::Subscriber.add(auth, list_id, 'js@mike.com', @fname, custom_fields, true, true)

you can try with heredoc : 您可以尝试使用heredoc

response = HTTParty.post(url, 
  :basic_auth => auth, :body => <<-BODY_CONTENT
  {
        "EmailAddress":"js@mike.com",
        "Name":"#{@fname}",
        "CustomFields":[
              {
                        "Key":"firstName",
                        "Value":"#{@fname}"
                    },
                    {
                      "Key":"country",
                        "Value":"#{@country}"
                    }
                    ],
        "Resubscribe":true,
        "RestartSubscriptionBasedAutoresponders":true
  }
BODY_CONTENT
)

You can use heredoc as explained here 您可以按照此处的说明使用heredoc

Double-quoting rules are also followed if you put double quotes around the identifier. 如果在标识符周围加上双引号,也会遵循双引号规则。 However, do not put double quotes around the terminator. 但是,不要在终止符两边加上双引号。

puts <<"QUIZ"
Student: #{name}

1.\tQuestion: What is 4+5?
\tAnswer: The sum of 4 and 5 is #{4+5}
QUIZ

You can use here documents: 您可以在此处使用以下文件:

name = 'John'
<<EOS
This is #{name}
EOS

Alternatively you can use flexible quotes, they can handle both ' and " characters: 另外,您可以使用灵活的引号,它们可以处理'"字符:

name = 'John'
%{
This is #{name}
}

Flexible quoting works with %() and %!! 灵活的报价适用于%()%!! as well. 也一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM