parseJSONObject()

本参考资料用于Processing 3.0+。 如果你使用的是前一版本,请在使用软件帮助菜单内的参考资料。. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Core Javadoc and Libraries Javadoc.

名称

parseJSONObject()

示例
 String data = "{ \"id\": 0, \"species\": \"Panthera leo\", \"name\": \"Lion\"  }";



void setup()
{  
JSONObject json = parseJSONObject(data);


if (json == null)
{
println("JSONObject could not be parsed");


  } else{
String species = json.getString("species");


println(species);


   }
  }  // Sketch prints: // Panthera leo 
描述 Takes a String, parses its contents, and returns a JSONObject. If the String does not contain JSONObject data or cannot be parsed, a null value is returned.

parseJSONObject() is most useful when pulling data dynamically, such as from third-party APIs. Normally, API results would be saved to a String, and then can be converted to a structured JSONObject using parseJSONObject(). Be sure to check if null is returned before performing operations on the new JSONObject in case the String content could not be parsed.

If your data already exists as a JSON file in the data folder, it is simpler to use loadJSONObject().

语法
parseJSONObject(input)
参数
input String: String to parse as a JSONObject
返回值 JSONObject
相关 loadJSONObject()
saveJSONObject()

Updated on October 2, 2018 05:02:06pm EDT

parseJSONObject()