JuliaIO / JSON.jl

JSON parsing and printing
Other
311 stars 100 forks source link

Why this json string cannot be parsed? #365

Closed NeroBlackstone closed 4 months ago

NeroBlackstone commented 4 months ago
{
    "contents": "While \u003Ca href=\"https://www.pcgamer.com/uk/search/?searchTerm=team+fortress+2\" "
}
using JSON

json = """
{
    "contents": "While \u003Ca href=\"https://www.pcgamer.com/uk/search/?searchTerm=team+fortress+2\" "
}
"""

JSON.parse(json)

Then throw a error:

ERROR: Expected ',' here
Line: 1
Around: ...<a href="https://www.pcgamer.c...
                    ^

Stacktrace:
 [1] _error(message::String, ps::JSON.Parser.MemoryParserState)
   @ JSON.Parser ~/.julia/packages/JSON/93Ea8/src/Parser.jl:140
 [2] _error_expected_char(c::UInt8, ps::JSON.Parser.MemoryParserState)
   @ JSON.Parser ~/.julia/packages/JSON/93Ea8/src/Parser.jl:83
 [3] skip!
   @ ~/.julia/packages/JSON/93Ea8/src/Parser.jl:80 [inlined]
 [4] parse_object(pc::JSON.Parser.ParserContext{Dict{String, Any}, Int64, true, nothing}, ps::JSON.Parser.MemoryParserState)
   @ JSON.Parser ~/.julia/packages/JSON/93Ea8/src/Parser.jl:234
 [5] parse_value(pc::JSON.Parser.ParserContext{Dict{String, Any}, Int64, true, nothing}, ps::JSON.Parser.MemoryParserState)
   @ JSON.Parser ~/.julia/packages/JSON/93Ea8/src/Parser.jl:166
 [6] parse(str::String; dicttype::Type, inttype::Type{Int64}, allownan::Bool, null::Nothing)
   @ JSON.Parser ~/.julia/packages/JSON/93Ea8/src/Parser.jl:450
 [7] parse(str::String)
   @ JSON.Parser ~/.julia/packages/JSON/93Ea8/src/Parser.jl:443
 [8] top-level scope
   @ REPL[4]:1

Why? I think this is a validated json string.

yuyichao commented 4 months ago

This is just julia string parsing. The \" in your string literal resulted in just a ".

julia> using JSON

julia> json = """
       {
               "contents": "While \u003Ca href=\"https://www.pcgamer.com/uk/search/?searchTerm=team+fortress+2\" "
       }
       """;

julia> json2 = """
       {
               "contents": "While \u003Ca href=\\"https://www.pcgamer.com/uk/search/?searchTerm=team+fortress+2\\" "
       }
       """;

julia> println(json)
{
        "contents": "While <a href="https://www.pcgamer.com/uk/search/?searchTerm=team+fortress+2" "
}

julia> println(json2)
{
        "contents": "While <a href=\"https://www.pcgamer.com/uk/search/?searchTerm=team+fortress+2\" "
}

julia> JSON.parse(json)
ERROR: Expected ',' here
Line: 1
Around: ...<a href="https://www.pcgamer.c...
                    ^

Stacktrace:
<......>

julia> JSON.parse(json2)
Dict{String, Any} with 1 entry:
  "contents" => "While <a href=\"https://www.pcgamer.com/uk/search/?searchTerm=tea…