Skip to content

Maybe a bug about string null of JSONObject #49

@wangxu0

Description

@wangxu0

A json string, maybe the user's name just is null:

{
  "name": "null",
  "mobile": "123"
}

Parsing:

JSONObject json = JSONObject.fromObject(jsonStr);
System.out.println(json.getString("name"));

Output:

“null”

But expected:

null

I read the source code, the string null is considered a JSON, So quote are added to both ends of the string null.

if( quoted && v instanceof String && (JSONUtils.mayBeJSON( (String) v ) || JSONUtils.isFunction( v ))){
       v = JSONUtils.DOUBLE_QUOTE + v + JSONUtils.DOUBLE_QUOTE;
}
public static boolean mayBeJSON( String string ) {
      return string != null
            && ("null".equals( string )
                  || (string.startsWith( "[" ) && string.endsWith( "]" )) || (string.startsWith( "{" ) && string.endsWith( "}" )));
   }

If this is a bug, I am happy to fix it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions