gpt4 book ai didi

android - 如何将转义的 JSON 行传输到 bean 列表

转载 作者:行者123 更新时间:2023-11-29 23:17:23 25 4
gpt4 key购买 nike

Java POJO :

import android.os.Parcel;
import android.os.Parcelable;

import com.gongzelong.duolingowordsearch.utils.ParcelableUtils;
import com.google.gson.annotations.SerializedName;

import java.util.List;

public class WordSearch implements Parcelable {
public static final Parcelable.Creator<WordSearch> CREATOR =
new Parcelable.Creator<WordSearch>() {
public WordSearch createFromParcel(Parcel source) {
return new WordSearch(source);
}

public WordSearch[] newArray(int size) {
return new WordSearch[size];
}
};
public final String word;
public final @SerializedName("character_grid") String[][] characterGrid;
public final @SerializedName("word_locations") List<String> translations;
public final @SerializedName("source_language") String sourceLanguage;
public final @SerializedName("target_language") String targetLanguage;

WordSearch(String sourceLanguage, String targetLanguage, String word,
String[][] characterGrid, List<String> translations) {
this.sourceLanguage = sourceLanguage;
this.targetLanguage = targetLanguage;
this.word = word;
this.characterGrid = characterGrid;
this.translations = translations;
}

private WordSearch(Parcel in) {
this.sourceLanguage = in.readString();
this.targetLanguage = in.readString();
this.word = in.readString();
this.characterGrid = ParcelableUtils.createStringArrayArray(in);
this.translations = in.createStringArrayList();
}

@Override public int describeContents() {
return 0;
}

@Override public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.sourceLanguage);
dest.writeString(this.targetLanguage);
dest.writeString(this.word);
dest.writeInt(this.characterGrid.length);
ParcelableUtils.writeStringArrayArray(dest, characterGrid);
dest.writeStringList(this.translations);
}
}

这是我从服务器得到的,不是JSON格式而是 xml 格式。

enter image description here enter image description here

"{\"source_language\": \"en\", \"word\": \"man\", \"character_grid\": [[\"i\", \"q\", \"\\u00ed\", \"l\", \"n\", \"n\", \"m\", \"\\u00f3\"], [\"f\", \"t\", \"v\", \"\\u00f1\", \"b\", \"m\", \"h\", \"a\"], [\"h\", \"j\", \"\\u00e9\", \"t\", \"e\", \"t\", \"o\", \"z\"], [\"x\", \"\\u00e1\", \"o\", \"i\", \"e\", \"\\u00f1\", \"m\", \"\\u00e9\"], [\"q\", \"\\u00e9\", \"i\", \"\\u00f3\", \"q\", \"s\", \"b\", \"s\"], [\"c\", \"u\", \"m\", \"y\", \"v\", \"l\", \"r\", \"x\"], [\"\\u00fc\", \"\\u00ed\", \"\\u00f3\", \"m\", \"o\", \"t\", \"e\", \"k\"], [\"a\", \"g\", \"r\", \"n\", \"n\", \"\\u00f3\", \"s\", \"m\"]], \"word_locations\": {\"6,1,6,2,6,3,6,4,6,5,6,6\": \"hombre\"}, \"target_language\": \"es\"}\n" +
"{\"source_language\": \"en\", \"word\": \"woman\", \"character_grid\": [[\"v\", \"\\u00e1\", \"q\", \"t\", \"b\", \"f\", \"q\"], [\"y\", \"x\", \"i\", \"a\", \"\\u00fc\", \"v\", \"a\"], [\"r\", \"d\", \"y\", \"\\u00ed\", \"t\", \"n\", \"a\"], [\"f\", \"v\", \"\\u00f3\", \"w\", \"l\", \"a\", \"v\"], [\"b\", \"u\", \"\\u00fa\", \"j\", \"q\", \"h\", \"\\u00e1\"], [\"c\", \"o\", \"m\", \"u\", \"j\", \"e\", \"r\"], [\"h\", \"o\", \"d\", \"\\u00fa\", \"w\", \"d\", \"\\u00fc\"]], \"word_locations\": {\"2,5,3,5,4,5,5,5,6,5\": \"mujer\"}, \"target_language\": \"es\"}\n" +
"{\"source_language\": \"en\", \"word\": \"boy\", \"character_grid\": [[\"x\", \"c\", \"e\", \"x\", \"c\", \"i\", \"o\"], [\"e\", \"z\", \"q\", \"r\", \"h\", \"w\", \"y\"], [\"\\u00f1\", \"\\u00e9\", \"\\u00f1\", \"w\", \"i\", \"n\", \"\\u00e1\"], [\"o\", \"l\", \"a\", \"\\u00e1\", \"c\", \"i\", \"n\"], [\"r\", \"v\", \"\\u00f1\", \"s\", \"o\", \"\\u00f1\", \"w\"], [\"k\", \"m\", \"w\", \"a\", \"\\u00fc\", \"o\", \"w\"], [\"\\u00f3\", \"r\", \"\\u00fa\", \"b\", \"l\", \"g\", \"\\u00fa\"]], \"word_locations\": {\"5,2,5,3,5,4,5,5\": \"ni\\u00f1o\", \"4,0,4,1,4,2,4,3,4,4\": \"chico\"}, \"target_language\": \"es\"}\n" +
"{\"source_language\": \"en\", \"word\": \"girl\", \"character_grid\": [[\"o\", \"s\", \"\\u00f3\", \"x\", \"h\", \"\\u00f1\", \"h\"], [\"\\u00fc\", \"r\", \"g\", \"o\", \"l\", \"\\u00fa\", \"b\"], [\"a\", \"t\", \"c\", \"h\", \"i\", \"c\", \"a\"], [\"u\", \"\\u00fa\", \"r\", \"w\", \"\\u00e1\", \"t\", \"\\u00e9\"], [\"p\", \"n\", \"v\", \"r\", \"q\", \"m\", \"l\"], [\"f\", \"d\", \"t\", \"e\", \"a\", \"\\u00f3\", \"l\"], [\"u\", \"t\", \"n\", \"i\", \"\\u00f1\", \"a\", \"s\"]], \"word_locations\": {\"2,2,3,2,4,2,5,2,6,2\": \"chica\", \"2,6,3,6,4,6,5,6\": \"ni\\u00f1a\"}, \"target_language\": \"es\"}\n" +
"{\"source_language\": \"en\", \"word\": \"am\", \"character_grid\": [[\"d\", \"c\", \"e\", \"h\", \"p\"], [\"f\", \"e\", \"\\u00fc\", \"p\", \"t\"], [\"s\", \"s\", \"\\u00f3\", \"\\u00ed\", \"l\"], [\"o\", \"s\", \"\\u00ed\", \"\\u00f1\", \"a\"], [\"y\", \"g\", \"i\", \"o\", \"n\"]], \"word_locations\": {\"0,2,0,3,0,4\": \"soy\"}, \"target_language\": \"es\"}\n" +
"{\"source_language\": \"en\", \"word\": \"she\", \"character_grid\": [[\"z\", \"n\", \"w\", \"f\", \"m\", \"\\u00e9\"], [\"d\", \"\\u00f3\", \"q\", \"w\", \"n\", \"e\"], [\"z\", \"\\u00e1\", \"v\", \"e\", \"\\u00f3\", \"l\"], [\"r\", \"c\", \"z\", \"z\", \"m\", \"l\"], [\"\\u00fc\", \"m\", \"\\u00e1\", \"\\u00fc\", \"n\", \"a\"], [\"e\", \"a\", \"e\", \"x\", \"\\u00f1\", \"h\"]], \"word_locations\": {\"5,1,5,2,5,3,5,4\": \"ella\"}, \"target_language\": \"es\"}\n" +
"{\"source_language\": \"en\", \"word\": \"apple\", \"character_grid\": [[\"\\u00fa\", \"k\", \"\\u00fc\", \"b\", \"\\u00ed\", \"n\", \"z\", \"d\", \"o\"], [\"u\", \"\\u00e1\", \"n\", \"g\", \"e\", \"y\", \"z\", \"o\", \"\\u00f1\"], [\"o\", \"\\u00e9\", \"\\u00fa\", \"\\u00e1\", \"v\", \"e\", \"x\", \"u\", \"m\"], [\"c\", \"w\", \"d\", \"z\", \"t\", \"k\", \"m\", \"l\", \"a\"], [\"u\", \"b\", \"o\", \"w\", \"\\u00ed\", \"a\", \"u\", \"q\", \"n\"], [\"g\", \"s\", \"m\", \"e\", \"c\", \"n\", \"k\", \"\\u00fa\", \"z\"], [\"a\", \"o\", \"v\", \"t\", \"p\", \"\\u00fa\", \"\\u00e9\", \"k\", \"a\"], [\"f\", \"j\", \"i\", \"j\", \"n\", \"i\", \"b\", \"\\u00f3\", \"n\"], [\"s\", \"q\", \"l\", \"j\", \"j\", \"f\", \"q\", \"g\", \"a\"]], \"word_locations\": {\"8,2,8,3,8,4,8,5,8,6,8,7,8,8\": \"manzana\"}, \"target_language\": \"es\"}\n" +
"{\"source_language\": \"en\", \"word\": \"eat\", \"character_grid\": [[\"i\", \"a\", \"c\", \"j\", \"r\", \"w\"], [\"k\", \"b\", \"n\", \"o\", \"u\", \"v\"], [\"v\", \"x\", \"z\", \"f\", \"m\", \"a\"], [\"u\", \"l\", \"o\", \"p\", \"e\", \"o\"], [\"l\", \"\\u00fa\", \"\\u00e9\", \"q\", \"j\", \"e\"], [\"a\", \"h\", \"\\u00fa\", \"l\", \"k\", \"w\"]], \"word_locations\": {\"2,0,3,1,4,2,5,3\": \"como\"}, \"target_language\": \"es\"}\n" +
"{\"source_language\": \"en\", \"word\": \"bread\", \"character_grid\": [[\"\\u00fc\", \"\\u00e1\", \"p\", \"a\", \"n\"], [\"k\", \"a\", \"k\", \"m\", \"l\"], [\"a\", \"x\", \"q\", \"e\", \"h\"], [\"p\", \"s\", \"a\", \"j\", \"\\u00ed\"], [\"\\u00e1\", \"q\", \"l\", \"j\", \"l\"]], \"word_locations\": {\"2,0,3,0,4,0\": \"pan\"}, \"target_language\": \"es\"}"

如何将字符串传输到ArrayList<WordSearch>

我试过如下但失败了:

Gson gson =
new GsonBuilder()
.registerTypeAdapter(WordSearch.class, new WordSearchDeserializer()).create();

String json =
"{\"source_language\": \"en\", \"word\": \"man\", \"character_grid\": [[\"i\", \"q\", \"\\u00ed\", \"l\", \"n\", \"n\", \"m\", \"\\u00f3\"], [\"f\", \"t\", \"v\", \"\\u00f1\", \"b\", \"m\", \"h\", \"a\"], [\"h\", \"j\", \"\\u00e9\", \"t\", \"e\", \"t\", \"o\", \"z\"], [\"x\", \"\\u00e1\", \"o\", \"i\", \"e\", \"\\u00f1\", \"m\", \"\\u00e9\"], [\"q\", \"\\u00e9\", \"i\", \"\\u00f3\", \"q\", \"s\", \"b\", \"s\"], [\"c\", \"u\", \"m\", \"y\", \"v\", \"l\", \"r\", \"x\"], [\"\\u00fc\", \"\\u00ed\", \"\\u00f3\", \"m\", \"o\", \"t\", \"e\", \"k\"], [\"a\", \"g\", \"r\", \"n\", \"n\", \"\\u00f3\", \"s\", \"m\"]], \"word_locations\": {\"6,1,6,2,6,3,6,4,6,5,6,6\": \"hombre\"}, \"target_language\": \"es\"}\n" +
"{\"source_language\": \"en\", \"word\": \"woman\", \"character_grid\": [[\"v\", \"\\u00e1\", \"q\", \"t\", \"b\", \"f\", \"q\"], [\"y\", \"x\", \"i\", \"a\", \"\\u00fc\", \"v\", \"a\"], [\"r\", \"d\", \"y\", \"\\u00ed\", \"t\", \"n\", \"a\"], [\"f\", \"v\", \"\\u00f3\", \"w\", \"l\", \"a\", \"v\"], [\"b\", \"u\", \"\\u00fa\", \"j\", \"q\", \"h\", \"\\u00e1\"], [\"c\", \"o\", \"m\", \"u\", \"j\", \"e\", \"r\"], [\"h\", \"o\", \"d\", \"\\u00fa\", \"w\", \"d\", \"\\u00fc\"]], \"word_locations\": {\"2,5,3,5,4,5,5,5,6,5\": \"mujer\"}, \"target_language\": \"es\"}\n" +
"{\"source_language\": \"en\", \"word\": \"boy\", \"character_grid\": [[\"x\", \"c\", \"e\", \"x\", \"c\", \"i\", \"o\"], [\"e\", \"z\", \"q\", \"r\", \"h\", \"w\", \"y\"], [\"\\u00f1\", \"\\u00e9\", \"\\u00f1\", \"w\", \"i\", \"n\", \"\\u00e1\"], [\"o\", \"l\", \"a\", \"\\u00e1\", \"c\", \"i\", \"n\"], [\"r\", \"v\", \"\\u00f1\", \"s\", \"o\", \"\\u00f1\", \"w\"], [\"k\", \"m\", \"w\", \"a\", \"\\u00fc\", \"o\", \"w\"], [\"\\u00f3\", \"r\", \"\\u00fa\", \"b\", \"l\", \"g\", \"\\u00fa\"]], \"word_locations\": {\"5,2,5,3,5,4,5,5\": \"ni\\u00f1o\", \"4,0,4,1,4,2,4,3,4,4\": \"chico\"}, \"target_language\": \"es\"}\n" +
"{\"source_language\": \"en\", \"word\": \"girl\", \"character_grid\": [[\"o\", \"s\", \"\\u00f3\", \"x\", \"h\", \"\\u00f1\", \"h\"], [\"\\u00fc\", \"r\", \"g\", \"o\", \"l\", \"\\u00fa\", \"b\"], [\"a\", \"t\", \"c\", \"h\", \"i\", \"c\", \"a\"], [\"u\", \"\\u00fa\", \"r\", \"w\", \"\\u00e1\", \"t\", \"\\u00e9\"], [\"p\", \"n\", \"v\", \"r\", \"q\", \"m\", \"l\"], [\"f\", \"d\", \"t\", \"e\", \"a\", \"\\u00f3\", \"l\"], [\"u\", \"t\", \"n\", \"i\", \"\\u00f1\", \"a\", \"s\"]], \"word_locations\": {\"2,2,3,2,4,2,5,2,6,2\": \"chica\", \"2,6,3,6,4,6,5,6\": \"ni\\u00f1a\"}, \"target_language\": \"es\"}\n" +
"{\"source_language\": \"en\", \"word\": \"am\", \"character_grid\": [[\"d\", \"c\", \"e\", \"h\", \"p\"], [\"f\", \"e\", \"\\u00fc\", \"p\", \"t\"], [\"s\", \"s\", \"\\u00f3\", \"\\u00ed\", \"l\"], [\"o\", \"s\", \"\\u00ed\", \"\\u00f1\", \"a\"], [\"y\", \"g\", \"i\", \"o\", \"n\"]], \"word_locations\": {\"0,2,0,3,0,4\": \"soy\"}, \"target_language\": \"es\"}\n" +
"{\"source_language\": \"en\", \"word\": \"she\", \"character_grid\": [[\"z\", \"n\", \"w\", \"f\", \"m\", \"\\u00e9\"], [\"d\", \"\\u00f3\", \"q\", \"w\", \"n\", \"e\"], [\"z\", \"\\u00e1\", \"v\", \"e\", \"\\u00f3\", \"l\"], [\"r\", \"c\", \"z\", \"z\", \"m\", \"l\"], [\"\\u00fc\", \"m\", \"\\u00e1\", \"\\u00fc\", \"n\", \"a\"], [\"e\", \"a\", \"e\", \"x\", \"\\u00f1\", \"h\"]], \"word_locations\": {\"5,1,5,2,5,3,5,4\": \"ella\"}, \"target_language\": \"es\"}\n" +
"{\"source_language\": \"en\", \"word\": \"apple\", \"character_grid\": [[\"\\u00fa\", \"k\", \"\\u00fc\", \"b\", \"\\u00ed\", \"n\", \"z\", \"d\", \"o\"], [\"u\", \"\\u00e1\", \"n\", \"g\", \"e\", \"y\", \"z\", \"o\", \"\\u00f1\"], [\"o\", \"\\u00e9\", \"\\u00fa\", \"\\u00e1\", \"v\", \"e\", \"x\", \"u\", \"m\"], [\"c\", \"w\", \"d\", \"z\", \"t\", \"k\", \"m\", \"l\", \"a\"], [\"u\", \"b\", \"o\", \"w\", \"\\u00ed\", \"a\", \"u\", \"q\", \"n\"], [\"g\", \"s\", \"m\", \"e\", \"c\", \"n\", \"k\", \"\\u00fa\", \"z\"], [\"a\", \"o\", \"v\", \"t\", \"p\", \"\\u00fa\", \"\\u00e9\", \"k\", \"a\"], [\"f\", \"j\", \"i\", \"j\", \"n\", \"i\", \"b\", \"\\u00f3\", \"n\"], [\"s\", \"q\", \"l\", \"j\", \"j\", \"f\", \"q\", \"g\", \"a\"]], \"word_locations\": {\"8,2,8,3,8,4,8,5,8,6,8,7,8,8\": \"manzana\"}, \"target_language\": \"es\"}\n" +
"{\"source_language\": \"en\", \"word\": \"eat\", \"character_grid\": [[\"i\", \"a\", \"c\", \"j\", \"r\", \"w\"], [\"k\", \"b\", \"n\", \"o\", \"u\", \"v\"], [\"v\", \"x\", \"z\", \"f\", \"m\", \"a\"], [\"u\", \"l\", \"o\", \"p\", \"e\", \"o\"], [\"l\", \"\\u00fa\", \"\\u00e9\", \"q\", \"j\", \"e\"], [\"a\", \"h\", \"\\u00fa\", \"l\", \"k\", \"w\"]], \"word_locations\": {\"2,0,3,1,4,2,5,3\": \"como\"}, \"target_language\": \"es\"}\n" +
"{\"source_language\": \"en\", \"word\": \"bread\", \"character_grid\": [[\"\\u00fc\", \"\\u00e1\", \"p\", \"a\", \"n\"], [\"k\", \"a\", \"k\", \"m\", \"l\"], [\"a\", \"x\", \"q\", \"e\", \"h\"], [\"p\", \"s\", \"a\", \"j\", \"\\u00ed\"], [\"\\u00e1\", \"q\", \"l\", \"j\", \"l\"]], \"word_locations\": {\"2,0,3,0,4,0\": \"pan\"}, \"target_language\": \"es\"}";

Type founderListType = new TypeToken<ArrayList<WordSearch>>(){}.getType();

List<WordSearch> founderList = gson.fromJson(json, founderListType);

最佳答案

您的“JSON”看起来不像 JSON。更像是转义 JSON 对象的列表。要正确反序列化它,您需要 unescape JSON 并删除开头和结尾的 " 字符。此外,word_locations/translations 不是列表 - 它是 Map。假设您的有效负载如下所示:

"{\"source_language\": \"en\", \"word\": \"man\", \"character_grid\": [[\"i\", \"q\", \"\\u00ed\", \"l\", \"n\", \"n\", \"m\", \"\\u00f3\"], [\"f\", \"t\", \"v\", \"\\u00f1\", \"b\", \"m\", \"h\", \"a\"], [\"h\", \"j\", \"\\u00e9\", \"t\", \"e\", \"t\", \"o\", \"z\"], [\"x\", \"\\u00e1\", \"o\", \"i\", \"e\", \"\\u00f1\", \"m\", \"\\u00e9\"], [\"q\", \"\\u00e9\", \"i\", \"\\u00f3\", \"q\", \"s\", \"b\", \"s\"], [\"c\", \"u\", \"m\", \"y\", \"v\", \"l\", \"r\", \"x\"], [\"\\u00fc\", \"\\u00ed\", \"\\u00f3\", \"m\", \"o\", \"t\", \"e\", \"k\"], [\"a\", \"g\", \"r\", \"n\", \"n\", \"\\u00f3\", \"s\", \"m\"]], \"word_locations\": {\"6,1,6,2,6,3,6,4,6,5,6,6\": \"hombre\"}, \"target_language\": \"es\"}"
"{\"source_language\": \"en\", \"word\": \"woman\", \"character_grid\": [[\"v\", \"\\u00e1\", \"q\", \"t\", \"b\", \"f\", \"q\"], [\"y\", \"x\", \"i\", \"a\", \"\\u00fc\", \"v\", \"a\"], [\"r\", \"d\", \"y\", \"\\u00ed\", \"t\", \"n\", \"a\"], [\"f\", \"v\", \"\\u00f3\", \"w\", \"l\", \"a\", \"v\"], [\"b\", \"u\", \"\\u00fa\", \"j\", \"q\", \"h\", \"\\u00e1\"], [\"c\", \"o\", \"m\", \"u\", \"j\", \"e\", \"r\"], [\"h\", \"o\", \"d\", \"\\u00fa\", \"w\", \"d\", \"\\u00fc\"]], \"word_locations\": {\"2,5,3,5,4,5,5,5,6,5\": \"mujer\"}, \"target_language\": \"es\"}"
"{\"source_language\": \"en\", \"word\": \"boy\", \"character_grid\": [[\"x\", \"c\", \"e\", \"x\", \"c\", \"i\", \"o\"], [\"e\", \"z\", \"q\", \"r\", \"h\", \"w\", \"y\"], [\"\\u00f1\", \"\\u00e9\", \"\\u00f1\", \"w\", \"i\", \"n\", \"\\u00e1\"], [\"o\", \"l\", \"a\", \"\\u00e1\", \"c\", \"i\", \"n\"], [\"r\", \"v\", \"\\u00f1\", \"s\", \"o\", \"\\u00f1\", \"w\"], [\"k\", \"m\", \"w\", \"a\", \"\\u00fc\", \"o\", \"w\"], [\"\\u00f3\", \"r\", \"\\u00fa\", \"b\", \"l\", \"g\", \"\\u00fa\"]], \"word_locations\": {\"5,2,5,3,5,4,5,5\": \"ni\\u00f1o\", \"4,0,4,1,4,2,4,3,4,4\": \"chico\"}, \"target_language\": \"es\"}"
"{\"source_language\": \"en\", \"word\": \"girl\", \"character_grid\": [[\"o\", \"s\", \"\\u00f3\", \"x\", \"h\", \"\\u00f1\", \"h\"], [\"\\u00fc\", \"r\", \"g\", \"o\", \"l\", \"\\u00fa\", \"b\"], [\"a\", \"t\", \"c\", \"h\", \"i\", \"c\", \"a\"], [\"u\", \"\\u00fa\", \"r\", \"w\", \"\\u00e1\", \"t\", \"\\u00e9\"], [\"p\", \"n\", \"v\", \"r\", \"q\", \"m\", \"l\"], [\"f\", \"d\", \"t\", \"e\", \"a\", \"\\u00f3\", \"l\"], [\"u\", \"t\", \"n\", \"i\", \"\\u00f1\", \"a\", \"s\"]], \"word_locations\": {\"2,2,3,2,4,2,5,2,6,2\": \"chica\", \"2,6,3,6,4,6,5,6\": \"ni\\u00f1a\"}, \"target_language\": \"es\"}"
"{\"source_language\": \"en\", \"word\": \"am\", \"character_grid\": [[\"d\", \"c\", \"e\", \"h\", \"p\"], [\"f\", \"e\", \"\\u00fc\", \"p\", \"t\"], [\"s\", \"s\", \"\\u00f3\", \"\\u00ed\", \"l\"], [\"o\", \"s\", \"\\u00ed\", \"\\u00f1\", \"a\"], [\"y\", \"g\", \"i\", \"o\", \"n\"]], \"word_locations\": {\"0,2,0,3,0,4\": \"soy\"}, \"target_language\": \"es\"}"
"{\"source_language\": \"en\", \"word\": \"she\", \"character_grid\": [[\"z\", \"n\", \"w\", \"f\", \"m\", \"\\u00e9\"], [\"d\", \"\\u00f3\", \"q\", \"w\", \"n\", \"e\"], [\"z\", \"\\u00e1\", \"v\", \"e\", \"\\u00f3\", \"l\"], [\"r\", \"c\", \"z\", \"z\", \"m\", \"l\"], [\"\\u00fc\", \"m\", \"\\u00e1\", \"\\u00fc\", \"n\", \"a\"], [\"e\", \"a\", \"e\", \"x\", \"\\u00f1\", \"h\"]], \"word_locations\": {\"5,1,5,2,5,3,5,4\": \"ella\"}, \"target_language\": \"es\"}"
"{\"source_language\": \"en\", \"word\": \"apple\", \"character_grid\": [[\"\\u00fa\", \"k\", \"\\u00fc\", \"b\", \"\\u00ed\", \"n\", \"z\", \"d\", \"o\"], [\"u\", \"\\u00e1\", \"n\", \"g\", \"e\", \"y\", \"z\", \"o\", \"\\u00f1\"], [\"o\", \"\\u00e9\", \"\\u00fa\", \"\\u00e1\", \"v\", \"e\", \"x\", \"u\", \"m\"], [\"c\", \"w\", \"d\", \"z\", \"t\", \"k\", \"m\", \"l\", \"a\"], [\"u\", \"b\", \"o\", \"w\", \"\\u00ed\", \"a\", \"u\", \"q\", \"n\"], [\"g\", \"s\", \"m\", \"e\", \"c\", \"n\", \"k\", \"\\u00fa\", \"z\"], [\"a\", \"o\", \"v\", \"t\", \"p\", \"\\u00fa\", \"\\u00e9\", \"k\", \"a\"], [\"f\", \"j\", \"i\", \"j\", \"n\", \"i\", \"b\", \"\\u00f3\", \"n\"], [\"s\", \"q\", \"l\", \"j\", \"j\", \"f\", \"q\", \"g\", \"a\"]], \"word_locations\": {\"8,2,8,3,8,4,8,5,8,6,8,7,8,8\": \"manzana\"}, \"target_language\": \"es\"}"
"{\"source_language\": \"en\", \"word\": \"eat\", \"character_grid\": [[\"i\", \"a\", \"c\", \"j\", \"r\", \"w\"], [\"k\", \"b\", \"n\", \"o\", \"u\", \"v\"], [\"v\", \"x\", \"z\", \"f\", \"m\", \"a\"], [\"u\", \"l\", \"o\", \"p\", \"e\", \"o\"], [\"l\", \"\\u00fa\", \"\\u00e9\", \"q\", \"j\", \"e\"], [\"a\", \"h\", \"\\u00fa\", \"l\", \"k\", \"w\"]], \"word_locations\": {\"2,0,3,1,4,2,5,3\": \"como\"}, \"target_language\": \"es\"}"
"{\"source_language\": \"en\", \"word\": \"bread\", \"character_grid\": [[\"\\u00fc\", \"\\u00e1\", \"p\", \"a\", \"n\"], [\"k\", \"a\", \"k\", \"m\", \"l\"], [\"a\", \"x\", \"q\", \"e\", \"h\"], [\"p\", \"s\", \"a\", \"j\", \"\\u00ed\"], [\"\\u00e1\", \"q\", \"l\", \"j\", \"l\"]], \"word_locations\": {\"2,0,3,0,4,0\": \"pan\"}, \"target_language\": \"es\"}"

下面的示例应用程序逐行读取此有效负载,反转义并反序列化为 POJO:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;

import java.io.File;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public class GsonApp {

public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.txt").getAbsoluteFile();

Stream<String> lines = Files.lines(jsonFile.toPath());

Gson gson = new GsonBuilder().create();

List<WordSearch> wordSearches = new ArrayList<>(128);
lines
.map(StringEscapeUtils::unescapeJson)
.map(l -> StringUtils.strip(l, "\""))
.forEach(line -> wordSearches.add(gson.fromJson(line, WordSearch.class)));

wordSearches.forEach(System.out::println);
}
}

class WordSearch {

private String word;

@SerializedName("character_grid")
private String[][] characterGrid;

@SerializedName("word_locations")
private Map<String, String> translations;

@SerializedName("source_language")
private String sourceLanguage;

@SerializedName("target_language")
private String targetLanguage;

public String getWord() {
return word;
}

public void setWord(String word) {
this.word = word;
}

public String[][] getCharacterGrid() {
return characterGrid;
}

public void setCharacterGrid(String[][] characterGrid) {
this.characterGrid = characterGrid;
}

public Map<String, String> getTranslations() {
return translations;
}

public void setTranslations(Map<String, String> translations) {
this.translations = translations;
}

public String getSourceLanguage() {
return sourceLanguage;
}

public void setSourceLanguage(String sourceLanguage) {
this.sourceLanguage = sourceLanguage;
}

public String getTargetLanguage() {
return targetLanguage;
}

public void setTargetLanguage(String targetLanguage) {
this.targetLanguage = targetLanguage;
}

@Override
public String toString() {
return "WordSearch{" +
"word='" + word + '\'' +
", translations=" + translations +
", sourceLanguage='" + sourceLanguage + '\'' +
", targetLanguage='" + targetLanguage + '\'' +
", characterGrid=" + toString(characterGrid) +
'}';
}

public String toString(String[][] grid) {
StringBuilder builder = new StringBuilder();
builder.append(System.lineSeparator());
Stream.of(grid).forEach(row -> builder.append(Arrays.toString(row)).append(System.lineSeparator()));

return builder.toString();
}
}

以上代码打印:

WordSearch{word='man', translations={6,1,6,2,6,3,6,4,6,5,6,6=hombre}, sourceLanguage='en', targetLanguage='es', characterGrid=
[i, q, í, l, n, n, m, ó]
[f, t, v, ñ, b, m, h, a]
[h, j, é, t, e, t, o, z]
[x, á, o, i, e, ñ, m, é]
[q, é, i, ó, q, s, b, s]
[c, u, m, y, v, l, r, x]
[ü, í, ó, m, o, t, e, k]
[a, g, r, n, n, ó, s, m]
}
WordSearch{word='woman', translations={2,5,3,5,4,5,5,5,6,5=mujer}, sourceLanguage='en', targetLanguage='es', characterGrid=
[v, á, q, t, b, f, q]
[y, x, i, a, ü, v, a]
[r, d, y, í, t, n, a]
[f, v, ó, w, l, a, v]
[b, u, ú, j, q, h, á]
[c, o, m, u, j, e, r]
[h, o, d, ú, w, d, ü]
}
WordSearch{word='boy', translations={5,2,5,3,5,4,5,5=niño, 4,0,4,1,4,2,4,3,4,4=chico}, sourceLanguage='en', targetLanguage='es', characterGrid=
[x, c, e, x, c, i, o]
[e, z, q, r, h, w, y]
[ñ, é, ñ, w, i, n, á]
[o, l, a, á, c, i, n]
[r, v, ñ, s, o, ñ, w]
[k, m, w, a, ü, o, w]
[ó, r, ú, b, l, g, ú]
}
WordSearch{word='girl', translations={2,2,3,2,4,2,5,2,6,2=chica, 2,6,3,6,4,6,5,6=niña}, sourceLanguage='en', targetLanguage='es', characterGrid=
[o, s, ó, x, h, ñ, h]
[ü, r, g, o, l, ú, b]
[a, t, c, h, i, c, a]
[u, ú, r, w, á, t, é]
[p, n, v, r, q, m, l]
[f, d, t, e, a, ó, l]
[u, t, n, i, ñ, a, s]
}
WordSearch{word='am', translations={0,2,0,3,0,4=soy}, sourceLanguage='en', targetLanguage='es', characterGrid=
[d, c, e, h, p]
[f, e, ü, p, t]
[s, s, ó, í, l]
[o, s, í, ñ, a]
[y, g, i, o, n]
}
WordSearch{word='she', translations={5,1,5,2,5,3,5,4=ella}, sourceLanguage='en', targetLanguage='es', characterGrid=
[z, n, w, f, m, é]
[d, ó, q, w, n, e]
[z, á, v, e, ó, l]
[r, c, z, z, m, l]
[ü, m, á, ü, n, a]
[e, a, e, x, ñ, h]
}
WordSearch{word='apple', translations={8,2,8,3,8,4,8,5,8,6,8,7,8,8=manzana}, sourceLanguage='en', targetLanguage='es', characterGrid=
[ú, k, ü, b, í, n, z, d, o]
[u, á, n, g, e, y, z, o, ñ]
[o, é, ú, á, v, e, x, u, m]
[c, w, d, z, t, k, m, l, a]
[u, b, o, w, í, a, u, q, n]
[g, s, m, e, c, n, k, ú, z]
[a, o, v, t, p, ú, é, k, a]
[f, j, i, j, n, i, b, ó, n]
[s, q, l, j, j, f, q, g, a]
}
WordSearch{word='eat', translations={2,0,3,1,4,2,5,3=como}, sourceLanguage='en', targetLanguage='es', characterGrid=
[i, a, c, j, r, w]
[k, b, n, o, u, v]
[v, x, z, f, m, a]
[u, l, o, p, e, o]
[l, ú, é, q, j, e]
[a, h, ú, l, k, w]
}
WordSearch{word='bread', translations={2,0,3,0,4,0=pan}, sourceLanguage='en', targetLanguage='es', characterGrid=
[ü, á, p, a, n]
[k, a, k, m, l]
[a, x, q, e, h]
[p, s, a, j, í]
[á, q, l, j, l]
}

另见:

关于android - 如何将转义的 JSON 行传输到 bean 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55089762/

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