Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maybe a bug about string null of JSONObject #49

Open
wangxu0 opened this issue Oct 25, 2018 · 1 comment
Open

Maybe a bug about string null of JSONObject #49

wangxu0 opened this issue Oct 25, 2018 · 1 comment

Comments

@wangxu0
Copy link

wangxu0 commented Oct 25, 2018

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.

@Alanscut
Copy link
Contributor

hey, if you expected the value is null, then the null of json string should not be wrapped by quotes, instead as follows:

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

and when get the value of "name", we should use json.get("name") rather than json.getString("name")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants