Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在API调用中生成JSON主体的正确语法_Ruby On Rails_Json - Fatal编程技术网

Ruby on rails 在API调用中生成JSON主体的正确语法

Ruby on rails 在API调用中生成JSON主体的正确语法,ruby-on-rails,json,Ruby On Rails,Json,使用以下长控制器操作代码 @available = Available.find(694) @tareservation_id = 8943 @request_date_time = Time.now.utc.iso8601 @request_id = Time.now.to_i @in_date = (Date.today + 24.days).strftime("%Y-%m-%d").to_s @book = %Q|{ "booking": { "currencyCode":

使用以下长控制器操作代码

@available = Available.find(694)
@tareservation_id = 8943
@request_date_time = Time.now.utc.iso8601
@request_id = Time.now.to_i
@in_date = (Date.today + 24.days).strftime("%Y-%m-%d").to_s
@book = %Q|{ 
  "booking": {
    "currencyCode": "USD",  
    "languageCode": "es",  
    "paxNationality": "ES",
    "clientRef": {
      "value": \"#{@tareservation_id}\",
      "mustBeUnique": true
    },
    "items": [
      {
        "itemNumber": 1,
        "immediateConfirmationRequired": true,
        "productCode": \"#{@available.product_code}\",
        "leadPaxName": 
          { "firstName": "Guy", 
            "lastName": "Test"
          },
        "product": 
          {
            "period": 
            {
              "start": "2018-08-27",
              "quantity": 2
            }
          }
        } ]
  },
  "requestAuditInfo": 
    { "agentCode": "001", 
      "requestPassword": "pass",
      "requestDateTime":  \"#{@requestDateTime}\",  
      "requestID": #{@request_id}    }, 
  "versionNumber": "2.0"
  }|
然后必须在主体调用中将其作为JSON发送到API

@result = HTTParty.post(
  'https://test.com/search', 
  :body => JSON.parse(@book).to_json,
  headers: {
    'Content-Type' => 'application/json', 
    'Accept' => 'application/json',
    'Connection' => 'Keep-Alive'
  }
)
如果删除了以下块:

        ,
        "product": 
          {
            "period": 
            {
              "start": "2018-08-27",
              "quantity": 2
            }
          }
在控制台
JSON.parse(@start)
中,正确解析。使用block
JSON::parserror:784:unexpected-token
。但我看不出这里有什么不正确的地方


Rails为将来的JSON转换处理字符串真的对语法很严格吗,特别是因为需要解释实例变量(包括字符串和整数)以及har返回?那会是什么?或者有没有更安全的解决方案来摆脱快速流沙?

事实证明,将太多的行粘贴到控制台(在本例中为iTerm2)会对内存产生影响。在行为符合预期的情况下,一次粘贴25行代码是观察到的最大值。

在联机JSON验证器中检查JSON是否有效。是的,JSON使用直接字符串或整数数据进行验证。那么为什么
JSON.parse
会变得不稳定呢?为什么不使用Jbuilder模板来创建JSON呢?这是一个想法。首先,我试图理解为什么rails的JSON解析会阻塞明显正确的数据。其次,我主要从视图使用jbuilder。在控制器上下文中提供上述示例以供接受/投票。