本文介绍了gson在json值内从逗号分隔值字符串解析项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 gson 解析 json 字符串。它类似于:

I am parsing a json string using gson. It is similar to this:

{
    "ACode": "aa",
    "RCode": "rr",
    "Errors": "e1,e2,e3"
}


$ b b

我认为错误应该是一个适当的 json 数组,但我没有控制。

I think that the errors should have been a proper json array, but I don't have control of that.

我想将错误导入 java 中的数组或集合。这很容易使用 String.split 以逗号作为分隔符。但是我是新的 gson ,我不知道我是否会忽略它提供的功能,以解析逗号分隔的字符串。

I want to get the errors into an array or collection in java. This is easy enough using String.split with comma as separator. However I am new to gson and I don't know if I would be ignoring functionality that it provides to parse a comma separated string.

有没有人知道 gson 能否自动处理?

Does anyone know whether gson can handle this automatically?

推荐答案

你可以创建一个变量为ACode,RCode,Errors的类,然后使用gson将其转换为该类,CustomObject obj2 = gson.fromJson(json,CustomObject。

you can create a class with variables "ACode","RCode","Errors" then use gson to convert it to that class, CustomObject obj2 = gson.fromJson(json, CustomObject .class);

这篇关于gson在json值内从逗号分隔值字符串解析项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 01:23