简体   繁体   English

如何使用数据对象代替String [] []

[英]How can I use Data Objects instead of a String[][]

I'm calling a RESTful api which gives me the following response: 我正在调用一个RESTful api,它会给我以下响应:

{
    "asks": [
        [
            "219.82",
            "2.19"
        ],
        [
            "219.83",
            "6.05"
        ]
    ],
    "bids": [
        [
            "219.40",
            "17.46"
        ],
        [
            "219.13",
            "53.93"
        ]
    ]
}

My data object implementation looks now like this: 我的数据对象实现现在看起来像这样:

public class OrderBook implements Serializable {

    /**
     * list containing the price and amount of all bid orders
     */
    private String[][] bids;

    /**
     * list containing the price and amount
     * of all ask orders
     */
    private String[][] asks;
}

How can I replace this String[][] with a data object and ensure that GSON can still parse it? 如何用数据对象替换String [] []并确保GSON仍然可以解析它?

The api is external and I cant there change anything. 该API是外部的,我无法更改任何内容。 The result is an array of an array with two properties. 结果是具有两个属性的数组的数组。 Instead of parsing this in java with GSON in a String[][], I'd like to have a Asks and Bids class. 我不想在Java中使用GSON在String [] []中对此进行解析,而是想要一个Asks and Bids类。 The Asks class should contain an array of the object Ask and the object Ask should have two variables: which are value and amount as String. Asks类应包含对象Ask的数组,而对象Ask应当具有两个变量:作为字符串的value和amount。 This is what I'd like to have in my java code. 这就是我想在我的Java代码中拥有的东西。

If I understand you correctly, you want to change the type of bids from String[][] to Object . 如果我对您的理解正确,则希望将出价类型从String[][]更改为Object

If you do that, you would have to look into creating a custom deserializer for that class. 如果这样做,则必须考虑为该类创建自定义解串器。 See here for details. 有关详细信息,请参见此处

But then: don't do that. 但是然后:不要那样做。 Why do you want give up on type safety? 为什么要放弃类型安全性? The quality of your Java source code does not improve when you willingly reduce the type information. 随意减少类型信息时,Java源代码的质量不会提高。

暂无
暂无

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

相关问题 如何使用字符串名称而不是字符串内容? - How can I use name of string instead of content of string? 如何在Eclipse BIRT中将Java对象用作数据集? - How can I use Java objects as data sets in Eclipse BIRT? 我有一个包含多个 JSON 对象的数据字符串,如何将字符串中的所有 JSON 对象存储在一个充满对象的数组中? - I have a data String with multiple JSON Objects, how can I store all JSON Objects from the String in an Array filled with objects? 如何在angular js中访问freemarker对象,或者我可以使用angular代替Freemarker - How to access freemarker objects in angular js or can i use angular instead of Freemarker 如何将 Pattern 和 Matcher 与 StringBuffer 或 char[] 类型而不是 String 一起使用? - How can I use Pattern and Matcher with StringBuffer or char[] types instead of String? 如何使用对象作为JSON中的键? - How can I use objects as keys in a JSON? 如何使用其他类中的对象? - How can I use objects in other classes? 如何将每个整数/双精度值从包含嵌套对象和 arrays 的 JSON 数据转换为字符串? - How can I convert each integer/double value to String from JSON data containing nested objects and arrays? 我可以使用JSONArray代替创建数据模型吗? - Can I use a JSONArray instead of creating a data model? 在 Java 8 中,我可以使用什么数据类型来代替“var”? - What data type can I use instead of 'var' in Java 8?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM