gpt4 book ai didi

java - Android - 使用 GSON 解析 JSON

转载 作者:太空狗 更新时间:2023-10-29 16:39:30 25 4
gpt4 key购买 nike

我想从 String 类型的 JSON 中解析数据。我正在使用谷歌 Gson。我想知道如何获取此 Json 字符串的“OriginalTerm”和“FirstTranslation”信息:

    {

"term0" : {
"PrincipalTranslations" : {
"0" :{
"OriginalTerm" : { "term" : "cat", "POS" : "n", "sense" : "domestic animal", "usage" : ""},
"FirstTranslation" : {"term" : "gato", "POS" : "nm", "sense" : " "}, "Note" : ""},
"1" :{
"OriginalTerm" : { "term" : "cat", "POS" : "n", "sense" : "member of cat family", "usage" : ""},
"FirstTranslation" : {"term" : "felino", "POS" : "nm", "sense" : "familia de animales"}, "Note" : ""}},
"AdditionalTranslations" : {
"0" :{
"OriginalTerm" : { "term" : "cat", "POS" : "n", "sense" : "guy", "usage" : "slang"},
"FirstTranslation" : {"term" : "tío, tipo, chaval", "POS" : "nm", "sense" : "coloq"},
"SecondTranslation" : {"term" : "vato", "POS" : "", "sense" : "Mex"}, "Note" : ""},

"original" : {
"Compounds" : {
"0" :{
"OriginalTerm" : { "term" : "alley cat", "POS" : "n", "sense" : "stray cat", "usage" : ""},
"FirstTranslation" : {"term" : "gato callejero", "POS" : "nm", "sense" : ""}, "Note" : ""},
"Lines" : "End Reached", "END" : true

}

我尝试按照这些信息进行操作,但无法解决:

http://albertattard.blogspot.com.es/2009/06/practical-example-of-gson.html

JSON parsing using Gson for Java

我尝试使用 GSON 使用 POJO 进行序列化,但我找不到正确的结构,我也尝试使用 JsonObject,跳过“term0”、“PrincipalTranslations”等对象键,但是当我有多个结果时我遇到了一些麻烦对于相同的 key ,例如:

    "0" :{
"OriginalTerm"....
"FirstTranslation"...
"1" :{
"OriginalTerm"....
"FirstTranslation"...
}

谢谢你的提前。

最佳答案

可以使用将 JSON 文本与对象一对一映射的 POJO 来使用 Gson 解析 JSON。但是,由于您只需要 JSON 字符串的一部分,您可以利用 JsonParser 对象,它允许您只获取 JSON 的一部分。

所以你可以获得 PrincipalTranslation 部分,然后应用 POJO 策略,记住你至少有两个结构:你的 Term 和两个 的组合术语 和注释(我称之为 Item)。

请记住,我编写的 POJO 不遵循 Java 命名约定,因此您可以添加 annotation使用不同的成员变量名称。

这是您可以粘贴并在您的 IDE 中运行以尝试的代码。

package stackoverflow.questions;

import java.lang.reflect.Type;
import java.util.*;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;

public class Q20337652 {

public static class Term {
String term;
String POS;
String sense;
String usage;

@Override
public String toString() {
return "Term [term=" + term + ", POS=" + POS + ", sense=" + sense + ", usage=" + usage + "]";
}


}

public static class Item {
Term OriginalTerm;
Term FirstTranslation;
String Note;

@Override
public String toString() {
return "Item [OriginalTerm=" + OriginalTerm + ", FirstTranslation=" + FirstTranslation + ", Note=" + Note + "]";
}


}



public static void main(String[] args){

String json =
" { "+
" "+
" \"term0\" : { "+
" \"PrincipalTranslations\" : { "+
" \"0\" :{ "+
" \"OriginalTerm\" : { \"term\" : \"cat\", \"POS\" : \"n\", \"sense\" : \"domestic animal\", \"usage\" : \"\"}, "+
" \"FirstTranslation\" : {\"term\" : \"gato\", \"POS\" : \"nm\", \"sense\" : \" \"}, \"Note\" : \"\"}, "+
" \"1\" :{ "+
" \"OriginalTerm\" : { \"term\" : \"cat\", \"POS\" : \"n\", \"sense\" : \"member of cat family\", \"usage\" : \"\"}, "+
" \"FirstTranslation\" : {\"term\" : \"felino\", \"POS\" : \"nm\", \"sense\" : \"familia de animales\"}, \"Note\" : \"\"}},"+
" \"AdditionalTranslations\" : { "+
" \"0\" :{ "+
" \"OriginalTerm\" : { \"term\" : \"cat\", \"POS\" : \"n\", \"sense\" : \"guy\", \"usage\" : \"slang\"}, "+
" \"FirstTranslation\" : {\"term\" : \"tío, tipo, chaval\", \"POS\" : \"nm\", \"sense\" : \"coloq\"}, "+
" \"SecondTranslation\" : {\"term\" : \"vato\", \"POS\" : \"\", \"sense\" : \"Mex\"}, \"Note\" : \"\"}, "+
" "+
" \"original\" : { "+
" \"Compounds\" : { "+
" \"0\" :{ "+
" \"OriginalTerm\" : { \"term\" : \"alley cat\", \"POS\" : \"n\", \"sense\" : \"stray cat\", \"usage\" : \"\"}, "+
" \"FirstTranslation\" : {\"term\" : \"gato callejero\", \"POS\" : \"nm\", \"sense\" : \"\"}, \"Note\" : \"\"}, "+
" \"Lines\" : \"End Reached\", \"END\" : true "+
" "+
" } "+
" } }}} ";
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(json);
JsonElement je2 = je.getAsJsonObject().get("term0");
JsonElement je3 = je2.getAsJsonObject().get("PrincipalTranslations");

Type mapType = new TypeToken<Map<String, Item>>() {}.getType();

Map<String, Item> principalTranslation = new Gson().fromJson(je3, mapType);

System.out.println(principalTranslation);


}

}

这是我的处决:

{0=Item [OriginalTerm=Term [term=cat, POS=n, sense=domestic animal, usage=], FirstTranslation=Term [term=gato, POS=nm, sense=  , usage=null], Note=], 
1=Item [OriginalTerm=Term [term=cat, POS=n, sense=member of cat family, usage=], FirstTranslation=Term [term=felino, POS=nm, sense=familia de animales, usage=null], Note=]}

关于java - Android - 使用 GSON 解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20337652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com