如何在Java中反序列化给定数据类型的n维JSON数组。

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

How to deserialize n-dimensional json array of a given data type in java

问题

无法编写通用代码,可以使用库(如gson、jackson等)将这些表示张量数据的JSON反序列化为Java数组。

示例JSON1
{
"dim": [1, 4],
"type": "FP64",
"data": [[5.1, 3.5, 1.4, 0.2]]
}

Java对象- List<List<Float>>

示例JSON2
{
"dim": [4],
"type": "INT8",
"data": [0, 1, 2, 3]
}

Java对象- List<Integer>

示例JSON3
{
"dim": 1,
"type": "BYTES",
"data": ["This is a String"]
}

Java对象- List<String>

英文:

Unable to write a generic code which can deserialize these json (represents tensor data) to java array using libraries like gson, jackson etc

example json1
{
"dim": [1,4],
"type": "FP64",
"data": [[5.1,3.5,1.4,0.2]]
}

Java object- List&lt;List&lt;&lt;Float&gt;&gt;

example json2
{
"dim": [4],
"type": "INT8",
"data": [0, 1, 2, 3]
}

Java object - List&lt;Integer&gt;

example json3
{
"dim": 1,
"type": "BYTES",
"data": ["This is a String"]
}

Java object- List&lt;String&gt;

答案1

得分: 1

你可以引入Maven依赖

<!--hutool-->
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.7.17</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.66</version>
    </dependency>

然后你可以编写代码,然后合并数据。

我的英语不太好,但解决方案是可行的。

关于hutool,你可以参考https://github.com/dromara/hutool/blob/v5-master/README-EN.md

英文:

You can introduce maven dependencies

&lt;!--hutool--&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;cn.hutool&lt;/groupId&gt;
        &lt;artifactId&gt;hutool-all&lt;/artifactId&gt;
        &lt;version&gt;5.7.17&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.alibaba&lt;/groupId&gt;
        &lt;artifactId&gt;fastjson&lt;/artifactId&gt;
        &lt;version&gt;1.2.66&lt;/version&gt;
    &lt;/dependency&gt;

Then you write the code
enter image description here

Then combine the data.

My English is not good,But the solution is OK.

For hutool, you can see https://github.com/dromara/hutool/blob/v5-master/README-EN.md

huangapple
  • 本文由 发表于 2023年3月8日 14:21:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75669935.html
匿名

发表评论

匿名网友

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

确定