- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我开发了一个 Android 应用程序,我必须反序列化一个 JSON 文件。我有这些类(class):
public class Medicine {
@SerializedName("substanta_activa")
private List<String> active_substance;
@SerializedName("produse")
private List<Product> product;
@SerializedName("dozaj")
private Dosage dosage;
@SerializedName("mentiuni")
private List<String> notes;
@SerializedName("cuvinte_cheie")
private List<String> keyword;
/* + getters and setters */
}
<小时/>
public class Product {
@SerializedName("denumire_comerciala")
private String productName;
@SerializedName("forme_de_prezentare")
private List<String> form;
/* + getters and setters */
}
<小时/>
public class Dosage {
@SerializedName("nounascuti")
private String newborn;
@SerializedName("copii")
private String child;
@SerializedName("adulti")
private String adult;
/* + getters and setters */
}
我有以下 JSON 文件:
[
{
"substanta_activa": [
"trimebutinum"
],
"produse": [
{
"denumire_comerciala": "Debridat",
"forme_de_prezentare": [
"susp. buvabilă",
"susp. 24mg/5ml în flac 250ml",
"compr 100mg"
]
},
{
"denumire_comerciala": "Ibutin",
"forme_de_prezentare": [
"compr 300mg"
]
},
{
"denumire_comerciala": "Trimebutin",
"forme_de_prezentare": [
"compr 100mg"
]
},
{
"denumire_comerciala": "Colperin",
"forme_de_prezentare": [
"compr 100mg"
]
}
],
"dozaj": {
"nounascuti": "1ml/kg/zi div 3,",
"copii": "1ml/kg/zi div 3, peste 5 ani 3x10ml",
"adulti": "3x1-2 compr/zi, 1x300mg/zi sau 3x1-2 lingură/zi"
},
"mentiuni": [
"se poate administra de la naștere",
"se poate administra amestecat cu apă, lapte",
"10ml conține 6g zahăr"
],
"cuvinte_cheie": [
"gastro",
"colică",
"dureri abdominale funcționale",
"constipație"
]
},
{
"substanta_activa": [
"benzydaminum"
],
"produse": [
{
"denumire_comerciala": "Tantum Verde comprimate",
"forme_de_prezentare": [
"pastile pt supt 3mg"
]
},
{
"denumire_comerciala": "Tantum Verde spray",
"forme_de_prezentare": [
"spray bucofaringian 0,15%, 0,3%"
]
}
],
"dozaj": {
"nounascuti": "contraindicat",
"copii": "2-6 ani: 2-6x1 puf/4kg; >6 sni: 2-6x 4doze sau 3x1 pastila/zi",
"adulti": "2-6x 4puf sau 3x1 pastila/zi"
},
"mentiuni": [
"se admin. max. 7 zile"
],
"cuvinte_cheie": [
"antiseptic, anestezic, antiinflamator, oral, OTC"
]
}
]
我尝试了多种方法,使用 GSON 或不使用 GSON,但都没有成功。感谢您提前提供的帮助。
编辑
更详细一点:我有一个 MainPageActivity,在其中初始化一个输入流、设置一个路径并从 JSONParser 类调用我的反序列化方法:
InputStream is = null;
String internalStoragePath = getApplicationContext().getFilesDir().getAbsolutePath();
File fileToInternalStorage = new File(internalStoragePath + "/medicinelist.json");
try {
is = new FileInputStream(fileToInternalStorage);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (is == null) {
is = getResources().openRawResource(R.raw.gyogyszerek);
}
jsonParser = new JSONParser();
try {
medicines = jsonParser.readJsonStream(getApplicationContext(), is);
//medicines = jsonParser.jsonDeserializer(getApplicationContext(), is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
在我的 JSONParser 类中,正如我所提到的,我尝试了多种方法来反序列化 JSON 输入。这是“传统”方式,使用 Android 内置的 JsonReader 类(抱歉,有点长):
public ArrayList readJsonStream(Context applicationContext, InputStream in) throws IOException {
JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
try {
return readMedicineArray(reader);
} finally {
reader.close();
}
}
public ArrayList readMedicineArray(JsonReader reader) throws IOException {
ArrayList medicines = new ArrayList();
reader.beginArray();
while (reader.hasNext()) {
medicines.add(readMedicine(reader));
}
reader.endArray();
return medicines;
}
public Medicine readMedicine(JsonReader reader) throws IOException {
List active_substance = null;
List product = null;
Dosage dosage = null;
List notes = null;
List keyword = null;
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("substanta_activa")) {
active_substance = readActiveSubstanceArray(reader);
} else if (name.equals("produse")) {
product = readProductArray(reader);
} else if (name.equals("dozaj")) {
dosage = readDosage (reader);
} else if (name.equals("mentiuni") && reader.peek() != JsonToken.NULL) {
notes= readNotesArray(reader);
} else if (name.equals("cuvinte_cheie") && reader.peek() != JsonToken.NULL) {
keyword = readKeywordArray(reader);
} else {
reader.skipValue();
}
}
reader.endObject();
return new Medicine(active_substance, product, dosage, notes, keyword);
}
public List readActiveSubstanceArray(JsonReader reader) throws IOException {
List active_substance = new ArrayList();
reader.beginArray();
while (reader.hasNext()) {
active_substance.add(reader.nextString());
}
reader.endArray();
return active_substance;
}
public List readProductArray(JsonReader reader) throws IOException {
List product = new ArrayList();
reader.beginArray();
while (reader.hasNext()) {
product.add(readProduct(reader));
}
reader.endArray();
return product;
}
public Product readProduct(JsonReader reader) throws IOException {
String productName = null;
List form = null;
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("denumire_comerciala")) {
productName = reader.nextString();
} else if (name.equals("forme_de_prezentare")) {
form = readFormArray(reader);
} else {
reader.skipValue();
}
}
reader.endObject();
return new Product(productName, form);
}
public List readFormArray(JsonReader reader) throws IOException {
List form = new ArrayList();
reader.beginArray();
while (reader.hasNext()) {
form.add(reader.nextString());
}
reader.endArray();
return form;
}
public Dosage readDosage(JsonReader reader) throws IOException {
String newborn= null;
String child= null;
String adult= null;
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("nounascuti")) {
newborn= reader.nextString();
} else if (name.equals("copii")) {
child= reader.nextString();
} else if (name.equals("adulti")) {
adult= reader.nextString();
} else {
reader.skipValue();
}
}
reader.endObject();
return new Dosage(newborn, child, adult);
}
public List readNotesArray(JsonReader reader) throws IOException {
List notes= new ArrayList();
reader.beginArray();
while (reader.hasNext()) {
notes.add(reader.nextString());
}
reader.endArray();
return notes;
}
public List readKeywordArray(JsonReader reader) throws IOException {
List keyword= new ArrayList();
reader.beginArray();
while (reader.hasNext()) {
keyword.add(reader.nextString());
}
reader.endArray();
return keyword;
}
这是 GSON 库的另一种方式:
public ArrayList<Medicine> jsonDeserializer(Context contexts, InputStream in) throws IOException {
Reader reader = new InputStreamReader(in);
ArrayList medicinesList = new ArrayList();
final GsonBuilder gsonBuilder = new GsonBuilder();
final Gson gson = gsonBuilder.create();
Medicine[] medicinesArray = new Gson().fromJson(reader, Medicine[].class);
for(int i = 0; i < medicinesArray.length; ++i){
medicinesList.add(medicinesArray[i]);
}
return medicinesList;
}
它们都不起作用,但我不知道问题出在哪里。
最佳答案
//试试这个
Gson gson = new Gson();
Type listType = new TypeToken<ArrayList<Medicine>>() {}.getType();
List<Medicine> medList = gson.fromJson(<YOUR JSON STRING>, listType);
关于java - 使用 GSON 反序列化 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29909822/
我试图让 gson 在 java 项目上工作,但每次运行时都会出现上述错误。我没有使用任何闪存 IDE,只是 vim,我看到的与我的问题相关的每个问题都与 eclipse 及其部署设置有关。我希望有人
我一直在关注将对象数组转换为 JSON 的教程,但我遇到了一些找不到解决方案的错误。 代码 第 60 - 64 行 Gson gson = new Gson().toJson(data); respo
我正在创建一个新的 Netty 管道,我正在尝试: 避免过早优化。 编写易于向我的一位实习生解释的代码。 这种工厂方法当然很容易解释: public String toJSON() { Gso
我尝试在程序中使用 Unirest,但不断收到此错误 java.lang.NoSuchMethodError: com.google.gson.Gson.newBuilder()Lcom/google
// Retrofit compile 'com.squareup.retrofit2:retrofit:2.1.0' // JSON Parsing compile 'com.google.code
我正在尝试将 Gson 与接口(interface)一起使用: public interface Photo { public int getWidth(); } public class D
我正在执行一个 Web 服务,这是我的响应,但是当我尝试使用 Gson 库将此 JSONObject(org.json.JSONObject) 转换为特定对象时,我的应用程序崩溃了。所以,我不知道为什
我有以下 .json 文件 - { "IDs":[ "1716136233", "2030187302", "204897807
我试图在我的项目中使用 GSON,但我的应用程序崩溃了,logcat 说找不到 com.google.gson.Gson。我已将 import com.google.gson.Gson 放在我的类文件
在下面的示例中,我尝试序列化我的自定义类Couple,其中包含Point2D 类型的字段。 我发现,SErialzing 的过程取决于 DEserializer 的存在。 此外,如果存在反序列化器,则
我有课 class ThreadComment( banned: Int, closed: Int, comment: String?, date: String?,
使用 gson 解析 json 时,我有一个奇怪的行为。我使用这个代码: private static Container parseContainer(String containerJson) {
我注意到 GSON HTML 转义 字符,可以通过使用disableHtmlEscaping()来禁用它构建器配置方法。但我的问题是 - 为什么 GSON 默认情况下会进行 HTML 转义?不进行 H
我注意到一个奇怪的问题。我可以使用 Junit 运行我的测试用例,但是当我使用 maven 运行时,其中一个测试用例失败。它提示找不到 Gson 类 def。 我能够在 Maven 依赖项中看到 Gs
我有一个看起来像这样的 json {response:{"status":{"....."},data:[{"name":"Alice","id":"123"},{"name":"Jack","id"
所以,我的应用已经发布了将近一年,但没有看到这个问题,现在它出现了。 即使是现在,我的手机上的调试版本也没有这个问题。我对从 Android Studio 打开的任何模拟器没有任何问题。然而,Goog
我是 Android 开发环境的新手,因此需要专家的帮助。 java.lang.NoClassDefFoundError: com.google.gson.Gson 项目中包含 Gson 库,但是当从
总结 我在我的 android 应用程序中使用 Retrofit 和 Gson 有一段时间了,但是我从服务器获得的 ID 是 13 位数字。 Gson 自动将这些数字转换为科学计数法,然后将其转换为
我正在使用一个大的 JSON 对象,它具有来自多个请求的响应。 我正在处理的部分只需要很少的对象,而且它们并不总是在前面。例如 json 结构是: ** json = { mainDocume
我有一个休息网络服务: @Path("/tranreq") public class TranscriptRequesterResource { private static final Gs
我是一名优秀的程序员,十分优秀!