JSON中get和opt的区别

2025-03-12 03:27:39
推荐回答(2个)
回答1:

  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.

回答2:

应该是post吧,这个是两种请求的区别,跟json没关系的