-
-
Notifications
You must be signed in to change notification settings - Fork 75
Maybe a bug about string null of JSONObject #49
Copy link
Copy link
Open
Description
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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels