Php 不同的JSON响应-{quot;count";:“123"}与{quot;count";=>;123";}
我有一些PHP代码,可以查询MySQL数据库中的计数 通过浏览器查询时,我得到以下输出:Php 不同的JSON响应-{quot;count";:“123"}与{quot;count";=>;123";},php,ruby,json,Php,Ruby,Json,我有一些PHP代码,可以查询MySQL数据库中的计数 通过浏览器查询时,我得到以下输出: {"count":"123"} 我还有一个Ruby脚本,它通过Net::HTTP执行相同的PHP脚本,但输出不同: {"count"=>"123"} 为什么会这样 //The URL uri = URI.parse("http://lab/count.php") http = Net::HTTP.new(uri.host, uri.port) //Request URL request = Net
{"count":"123"}
我还有一个Ruby脚本,它通过Net::HTTP执行相同的PHP脚本,但输出不同:
{"count"=>"123"}
为什么会这样
//The URL
uri = URI.parse("http://lab/count.php")
http = Net::HTTP.new(uri.host, uri.port)
//Request URL
request = Net::HTTP::Get.new(uri.request_uri)
//Basic authentication
request.basic_auth("user1", "secret")
response = http.request(request)
//Response
response = JSON.parse(response.body)
puts results
//Value 'count'
count = JSON.parse(response.body)[0]
puts count
谢谢。{“count”=>“123”}
不是JSON响应
它是哈希表的ruby文本
我认为您看到了解析JSON的结果:
>> require 'json'
>> JSON.parse('{"count":"123"}') # => {"count"=>"123"}
>> puts JSON.dump({"count"=>"123"}) # prints => {"count":"123"}
更新对评论的回应 要打印
123
uri = URI.parse("http://lab/count.php")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request.basic_auth("user1", "secret")
response = http.request(request)
response = JSON.parse(response.body)
puts response['count']
{“count”=>“123”}
不是JSON响应
它是哈希表的ruby文本
我认为您看到了解析JSON的结果:
>> require 'json'
>> JSON.parse('{"count":"123"}') # => {"count"=>"123"}
>> puts JSON.dump({"count"=>"123"}) # prints => {"count":"123"}
更新对评论的回应 要打印
123
uri = URI.parse("http://lab/count.php")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request.basic_auth("user1", "secret")
response = http.request(request)
response = JSON.parse(response.body)
puts response['count']
@pablo808,实际上您不需要解析JSON。从ruby脚本中删除json解析部分。谢谢。怎么能只打印值“123”。@pablo808,你能把代码再发一遍吗。(不是在评论中,而是在你的问题中)@pablo808,实际上你不需要解析JSON。从ruby脚本中删除json解析部分。谢谢。怎么能只打印值“123”。@pablo808,你能把代码再发一遍吗。(不是评论,而是你的问题)