设计 Java 中的 JSON

huangapple go评论65阅读模式
英文:

Design JSON in Java

问题

import java.util.Map;

public class MonthData {
    private Map<String, DayData> data;

    public Map<String, DayData> getData() {
        return data;
    }

    public void setData(Map<String, DayData> data) {
        this.data = data;
    }
}

public class DayData {
    private int A;
    private int B;

    public int getA() {
        return A;
    }

    public void setA(int A) {
        this.A = A;
    }

    public int getB() {
        return B;
    }

    public void setB(int B) {
        this.B = B;
    }
}

In your Spring Boot application, you can use these classes to parse the JSON data. You can create a REST API endpoint that accepts the JSON input and uses Spring's @RequestBody annotation to automatically convert the JSON into the MonthData object. Here's a simplified example:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ApiController {

    @PostMapping("/parse-json")
    public void parseJson(@RequestBody MonthData monthData) {
        // Do something with the parsed data
    }
}

Please make sure you have the necessary Spring Boot dependencies in your project for this to work.

英文:

I have an API which returns JSON in below format. Need to design Java class for the same. These values will be varying. So can't create a java class with fixed properties.
JSON as:

 {
        &quot;jan&quot;: {
            &quot;2020-01-01&quot;: {
                &quot;A&quot;: 348,
                &quot;B&quot;: 408
            },
            &quot;2020-01-02&quot;: {
                &quot;A&quot;: 348,
                &quot;B&quot;: 408   
            }
        },
        &quot;feb&quot;: {
            &quot;2020-02-01&quot;: {
                &quot;A&quot;: 348,
                &quot;B&quot;: 408
            },
            &quot;2020-02-02&quot;: {
                &quot;A&quot;: 348,
                &quot;B&quot;: 408
            }  
     
        }		
    }	

How to design Object in Spring Boot/Java for this?

答案1

得分: 2

我理解您正在寻找一个用于反序列化您分享的 JSON 结构的 Java 类。对我来说,这看起来像是:

Map<String, Map<String, Data>>

其中,

class Data {
   private int A;
   private int B;
}

现在,如果您想要使用例如 jackson 进行反序列化,您可以使用:

new ObjectMapper().readValue(myJson, new TypeReference<Map<String, Map<String, Data>>>() {});
英文:

I understand that you are looking for a java class to use to deserialize the JSON structure that you are sharing. This looks to me like

Map&lt;String, Map&lt;String, Data&gt;&gt; 

Where

class Data {
   private int A;
   private int B;
}

Now if you want to deserialize it using for example jackson, you can use

new ObjectMapper().readValue(myJson, new TypeReference&lt;Map&lt;String, Map&lt;String, Data&gt;&gt;&gt;() {});

huangapple
  • 本文由 发表于 2020年9月22日 16:02:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64005476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定