Creating Java Object with reserved keywords as variable name

后端 未结 3 1444
小蘑菇
小蘑菇 2020-12-12 00:08

I have JSON that needs to be converted to a Java Object. The JSONs I need to handle can look like this:

{
    \"documents\": [
        {
        \"title\": \         


        
3条回答
  •  星月不相逢
    2020-12-12 00:31

    In fact, it depends on the tool you are using. With tools mapping directly to your custom POJO (like GSON, Jackson), you need to map your JSON field name with your Java correct and valid field name.

    If you use a mors basic library such as JSON.org's, there is no need to do so because you parse it to specific object allowing you to handle it.

    JSONObject obj = new JSONObject(" .... ");
    
    JSONArray arr = obj.getJSONArray("documents");
    String abstractValue = arr.getJSONObject(0).getString("abstract");
    

提交回复
热议问题