简体   繁体   English

处理数组中的JSON响应日期

[英]Handle JSON response date in array

so I'm inserting the date of birth to an API, and the API returns the updated information, which i'm supposed to present on another page after handling the data in java backend. 因此,我向API插入了出生日期,并且该API返回了更新的信息,在处理Java后端中的数据后,我应该将其显示在另一页上。

Here is what gets returned in JSON: 这是JSON返回的内容:

"firstname": "John",
"middlename": "The",
  "lastname": "Doe",
  "displayName": "John The Doe",
  "dateOfBirth": [
    1994,
    3,
    26
  ]

so what I'm having trouble with, is picking out the 3 (year/month/day) in separate variables, because if theres no 0 in 03 ( mars for example ) i want to add the 0, same goes with day. 所以我遇到的问题是,在单独的变量中选择3(年/月/日),因为如果03中没有0(例如mars),我想将0加起来,那么一天也一样。

Here i'm getting the object: 在这里,我得到的对象:

@Override
public Object getDateOfBirth() {
    return get("dateOfBirth");
}

But i'm getting [1994,3,26] which obviously looks very bad displayed on a website. 但是我得到了[1994,3,26] ,显然,它在网站上显示的非常糟糕。

How would you get the 3 "1994,3,26" in different variables? 您将如何获得3个“ 1994,3,26”不同的变量?

Any help is greatly appreciated. 任何帮助是极大的赞赏。

if you are using Java 8 or later, you can use LocalDate . 如果您使用的是Java 8或更高版本,则可以使用LocalDate The LocalDate class represents a date-only value without time-of-day and without time zone. LocalDate类表示没有日期和时区的仅日期值。

LocalDate.of(year, month, day); //2015-12-22
LocalDate.of(Integer.parseInt(dateOfBirth[0]), Integer.parseInt(dateOfBirth[1]), Integer.parseInt(dateOfBirth[2]));

take a look on this link This can help. 看看这个链接,这可以帮助。 I can recommend you to use the Gson 我可以建议您使用Gson

if you use maven add this dependency to the pom 如果使用maven,则将此依赖项添加到pom中

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.0</version>
</dependency>



public class MyJsonAsClass
{
    private String firstname;
    private String middlename;
    private String lastname;
    private String displayName;
    private List<String> dateOfBirth;

    public MyJsonAsClass()
    {

    }

    public static MyJsonAsClass fromString(String json) 
    {
       Gson gson = new Gson();
       MyJsonAsClass info = gson.fromJson(json, MyJsonAsClass.class);
       return info;
    }
    /*you need to add getters & setters */

    public List<String> getdateOfBirth()
    {
       for (String date : dateOfBirth) {
           if (date.length()==1)
               date='0'+date;
       }
       return dateOfBirth;
    }

But i'm getting [1994,3,26] which obviously looks very bad displayed on a website. 但是我得到了[1994,3,26],显然,它在网站上显示的非常糟糕。

How would you get the 3 "1994,3,26" in different variables? 您将如何获得3个“ 1994,3,26”不同的变量?

You can transform the output of returned array object into custom format for display purpose. 您可以将返回的数组对象的输出转换为自定义格式以用于显示。 Assuming get("dateOfBirth") returns and Array object. 假设get("dateOfBirth")返回Array对象。 You can do something similar ... 您可以做类似的事情...

String result = Arrays.toString(dateOfBirthArrayObject).replaceAll("[\\[\\]]", "");

System.out.println(result);

Output: 1994, 3, 26 产出: 1994、3、26

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

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