简体   繁体   English

Android Java Spring JSON响应具有$ in键。 怎么解析?

[英]Android Java Spring JSON response has $ in key. How to parse?

I'm new to Java. 我是Java新手。 I'm using Spring to consume a REST api that outputs JSON. 我正在使用Spring来使用输出JSON的REST API。 With the tutorials on the Spring website I can easily have the JSON response converted to an object of my desired class. 通过Spring网站上的教程,我可以轻松地将JSON响应转换为所需类的对象。 The problem is now that one of the keys in the JSON response is $id . 现在的问题是,JSON响应中的键之一是$id I cannot make a variable with a dollar sign in it. 我不能用美元符号制作变量。 I assume I should define some configuration somewhere that such a name would be converted into something acceptable. 我假设我应该在某个位置定义一些配置,以便将这样的名称转换为可接受的名称。 I don't know how. 我不知道

My Rest request code: 我的休息请求代码:

protected LoginResult doInBackground(Void... params) {
    try {
        Log.i(TAG, "Making Login request");
        //TODO: Make this a setting
        final String url = "https://someurl.com/api/login";

        LoginCredentials login = new LoginCredentials("foo@bar.com", "qwerty123");

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

        LoginResult result = restTemplate.postForObject(url, login, LoginResult.class);

        Log.d(TAG, "Got the LoginResult.");
        return result;
    } catch (Exception e) {
        //TODO: Exception handling
        Log.e(TAG, e.getMessage(), e);
    }

    return null;
}

The resulting JSON looks something like this: 生成的JSON看起来像这样:

{
   "_id":{
      "$id":"98765432"
   },
   "name":"Person Guy",
   "email":"foo@bar.com",
   "roles":[
      "user"
   ],
   "active":true,
   "created":{
      "sec":1439117849,
      "usec":856000
   },
   "session":{
      "token":"12345678",
      "user_id":"98765432",
      "created":{
         "sec":1439134272,
         "usec":0
      },
      "last_extended":{
         "sec":1439134272,
         "usec":0
      },
      "expires":{
         "sec":1439998272,
         "usec":0
      }
   }
}

The $id part is where things get difficult. $id是困难的地方。 The LoginResult class looks like this: LoginResult类如下所示:

@JsonIgnoreProperties(ignoreUnknown = true)
public class LoginResult {
    private String name;
    private String email;
    private MongoId _id;
    /* Getters and setters */
}

The MongoId class looks like this (The JsonIgnoreProperties is now added to avoid exceptions): MongoId类看起来像这样(现在添加了JsonIgnoreProperties以避免出现异常):

@JsonIgnoreProperties(ignoreUnknown = true)
public class MongoId {
    private String id; //This is $id in the JSON response.

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

Any help would be largely appreciated. 任何帮助将不胜感激。

You can use the @JsonProperty("$id") annotation in MongoId to tell how the JSON is mapped to your Java object: 您可以使用@JsonProperty("$id")在注释MongoId告诉JSON是如何映射到Java对象:

@JsonIgnoreProperties(ignoreUnknown = true)
public class MongoId {
    @JsonProperty("$id") 
    private String id; //This is $id in the JSON response.

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

Here is a quick overview for reference. 这是一个快速概述,以供参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM