dewski / json_builder

Rails provides an excellent XML Builder by default to build RSS and ATOM feeds, but nothing to help you build complex and custom JSON data structures. JSON Builder is here to help.
http://garrettbjerkhoel.com/json_builder/
MIT License
244 stars 52 forks source link

JSON::ParseError #5

Closed swistaczek closed 12 years ago

swistaczek commented 13 years ago

Hello, I pasted my code and whole error msg at http://pastie.org/2302755

JSON::ParserError in Admin/users#index

I dont know what is going on, I am doing right the same like in readme.

Thanks in advantage for help.

chdorner commented 13 years ago

I have the exact same problem, the code shown in README actually produces wrong JSON.

arr = [1,2,3,4,5,6,7,8,9,0]
json = JSONBuilder::Generator.new
json.posts do
  json.array! arr do
    arr.each do |a|
      json.array_item! do
        json.id a
      end
    end
  end
end
puts json.compile!

The above code produces:

[{"posts":{"id":1},{"id": 2},{"id": 3},{"id": 4},{"id": 5},{"id": 6},{"id": 7},{"id": 8},{"id": 9},{"id": 0}}]

But it should produce:

{"posts":[{"id":1},{"id": 2},{"id": 3},{"id": 4},{"id": 5},{"id": 6},{"id": 7},{"id": 8},{"id": 9},{"id": 0}]}
dewski commented 12 years ago

I have fixed this with json_builder 3.0 which I am releasing shortly, which has a new and easier to use API. So to do that with the new API, it will be:

posts [1,2,3,4,5,6,7,8,9,0] do |i|
  id i
end