您的位置:首页 > Web前端 > JavaScript

JSON中get()和opt()的区别

2016-02-23 11:45 417 查看
get()取值不正确会抛出异常,必须用try catch或者throw包起

而opt()取值不正确则会试图进行转化或者输出友好值,不会抛出异常

opt

public java.lang.Object opt(java.lang.String key)

Get an optional value associated with a key.
 

Parameters:
key
 - A key string.Returns:An object which is the value, or null if there is no value.
 

optDouble

public double optDouble(java.lang.String key)

Get an optional double associated with a key, or NaN if there is no such key or if its value is not a number. If the value is a string, an attempt will be made to evaluate it as a number.

optBoolean

public boolean optBoolean(java.lang.String key)

Get an optional boolean associated with a key. It returns false if there is no such key, or if the value is not

optInt

public int optInt(java.lang.String key)

Get an optional int value associated with a key, or zero if there is no such key or if the value is not a number. If the value is a string, an attempt will be made to evaluate it as a number.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: