简体   繁体   English

如何在Ruby on Rails中使用单引号和双引号解析JSON

[英]how to parse JSON with single and double quotes in ruby on rails

I am Having an issue where if a name comes in with a ' or " my JSON parsing fails. I want to know how i can avoid this. 我遇到一个问题,如果名称带有'"我的JSON解析将失败。我想知道如何避免这种情况。

This is my current code: 这是我当前的代码:

def create

puts params

#@contact = Contact.new(contact_params)
#@contact.user = current_user

contactsParam = params[:contacts]

if contactsParam

  # contactsParam.each do |contact|
  # end

  contactValues = contactsParam.map { |c| 

    puts "dulce!!! : " + c.to_s

    json = JSON.parse(c)
    result = "('#{json['name']}', '#{json['phone_number']}', '#{json['detail']}', '#{json['image_url']}', '#{json['email']}', '#{json['address']}', '#{json['city']}', '#{json['state']}', '#{json['zipcode']}', '#{json['country']}', #{current_user.id}, now(), now())" 
    result
  }.join(",")

  if contactValues.length > 0
     ActiveRecord::Base.connection.execute("INSERT INTO contacts (name, phone_number, detail, image_url, email, address, city, state, zipcode, country, user_id, created_at, updated_at) VALUES #{contactValues}")
  end
end

here is an example of a JSON String that fails (i added a bunch of random characters to test). 这是一个失败的JSON字符串示例(我添加了一堆随机字符进行测试)。

   {"name":"Aaacontacttest'/'@"-jgg&$;$/&/@.'sheh","phone_number":",7*#;878545848487849648","detail":"","image_url":"","email":"test@test.com","address":"-/:;()$@""":$;$:$$/!:!/!,!6677bhgv
2017-07-25T20:08:54.614283+00:00 app[web.1]: Hsbsbsbb7788$!","city":"Hehshdbdb","state":"HSSHHSHS$&:$:$","zipcode":"3319)","country":"United States"}

1. 1。

Let's first correct the JSON Object which you gave: 首先让我们更正您提供的JSON对象:

  {
    "name":"Aaacontacttest'/'@"-jgg&$;$/&/@.'sheh",
    "phone_number":",7*#;878545848487849648",
    "detail":"",
    "image_url":"",
    "email":"test@test.com",
    "address":"-/:;()$@""":$;$:$$/!:!/!,!6677bhgv2017-07-25T20:08:54.614283+00:00 app[web.1]: Hsbsbsbb7788$!",
    "city":"Hehshdbdb",
    "state":"HSSHHSHS$&:$:$",
    "zipcode":"3319)",
    "country":"United States"
}

The problem with this JSON object is, it is not closed properly. 此JSON对象的问题是,它没有正确关闭。 If you wanted to use quotes in your object then please use Backslash to omit to close the value of the object. 如果要在对象中使用引号,请使用反斜杠省略以关闭对象的值。

Let's convert the above invalid JSON Object to valid JSON object first, such ruby avoid syntax error: 让我们首先将上述无效的JSON对象转换为有效的JSON对象 ,这样的ruby避免语法错误:

 {
    "name":"Aaacontacttest'/'@\"-jgg&$;$/&/@.'sheh",
    "phone_number":",7*#;878545848487849648",
    "detail":"",
    "image_url":"",
    "email":"test@test.com",
    "address":"-/:;()$@\"\"\":$;$:$$/!:!/!,!6677bhgv2017-07-25T20:08:54.614283+00:00 app[web.1]: Hsbsbsbb7788$!",
    "city":"Hehshdbdb",
    "state":"HSSHHSHS$&:$:$",
    "zipcode":"3319)",
    "country":"United States"
}

What we did over here: Initial Hash object with the "name" as the key has a value: 我们在这里所做的是:以“名称”作为键的初始Hash对象具有一个值:

"name": "Aaacontacttest'/'@" “名称”: “ Aaacontacttest'/'@” -jgg&$;$/&/@.'sheh" -jgg&$。$ /&/ @“佘诗曼”

Area with bold depicting that you have closed the value with quotes where rest of the Italic character is marked as invalid. 粗体字表示您已用引号将值封闭的区域,其余的斜体字符标记为无效。

Types of errors which ruby would be raised either "syntax error" or unexpected 'any char', expecting end-of-input 红宝石将引发“语法错误”或意外的“任何字符”的错误类型,期望输入结束

We have introduced backslash in the value to ensure proper closing of the value. 我们在值中引入了反斜杠,以确保正确关闭值。

"name": "Aaacontacttest'/'@\\" -jgg&$;$/&/@.'sheh" “名称”: “ Aaacontacttest'/'@ \\” -jgg&$; $ /&/ @。'sheh“

So where ever you have used quotes in your value please omit them using backslash. 因此,如果您在报价中使用过引号,请使用反斜杠将其忽略。

  1. The object which you gave is the normal hash object not a JSON object in case of ruby, as Ruby JSON parser expects the argument to be a string and can't convert objects like a hash or array. 您提供的对象是普通的哈希对象,在使用ruby的情况下不是JSON对象,因为Ruby JSON解析器期望参数为字符串,并且无法转换哈希或数组之类的对象。

If you do JSON.generate(hash) you will get the JSON object: 如果执行JSON.generate(hash) ,则将获得JSON对象:

Note: hash if the corrected hashobject. 注意:如果已更正的哈希对象,则为hash。

Adding JSON object over here. 在此处添加JSON对象。

"{\\"name\\":\\"Aaacontacttest'/'@\\\\"-jgg&$;$/&/@.'sheh\\",\\"phone_number\\":\\",7*#;878545848487849648\\",\\"detail\\":\\"\\",\\"image_url\\":\\"\\",\\"email\\":\\"test@test.com\\",\\"address\\":\\"-/:;()$@\\\\"\\\\"\\\\":$;$:$$/!:!/!,!6677bhgv2017-07-25T20:08:54.614283+00:00 app[web.1]: Hsbsbsbb7788$!\\",\\"city\\":\\"Hehshdbdb\\",\\"state\\":\\"HSSHHSHS$&:$:$\\",\\"zipcode\\":\\"3319)\\",\\"country\\":\\"United States\\"}" “{\\” 名称\\ “:\\” Aaacontacttest '/' @ \\\\ “ - 遗传学报&$,$ /&/ @”佘诗曼\\“,\\ ”PHONE_NUMBER \\“:\\”,7 *#; 878545848487849648 \\“, \\ “细节\\”:\\ “\\”,\\ “IMAGE_URL \\”:\\ “\\”,\\ “电子邮件\\”:\\ “test@test.com \\”,\\ “地址\\”:\\“ - / :; ()$ @ \\\\“ \\\\” \\\\“:$; $:$$ /!:!/!,!6677bhgv2017-07-25T20:08:54.614283 + 00:00 app [web.1]:Hsbsbsbb7788 $ !\\ “\\ ”城\\“:\\ ”Hehshdbdb \\“,\\ ”状态\\“:\\ ”HSSHHSHS $:$:$ \\“,\\ ”邮政编码\\“:\\ ”3319)\\“,\\” 国\\“:\\“美国\\”}”

If you do JSON.parse(HashObject not JSONobject) the ruby would throw an error: 如果您执行JSON.parse(HashObject不是JSONobject),则ruby会抛出错误:

TypeError: no implicit conversion of Hash into String TypeError:没有将Hash隐式转换为String

Summary: 摘要:

  1. Correct your hashObject. 更正您的hashObject。

  2. Convert you hashObject into the valid JSON object for ruby. 将您的hashObject转换为有效的ruby JSON对象。 If you wanted to generate JSON use JSON.generate(hash) method. 如果要生成JSON,请使用JSON.generate(hash)方法。

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

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