- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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 格式。
"{\"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]
}
另见:
StringEscapeUtils.unescapeJson
来自commons-text
StringUtils.strip
来自 commons-lang3
关于android - 如何将转义的 JSON 行传输到 bean 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55089762/
我有一个 javascript 从用户输入中读取的 URL。这是 JavaScript 代码的一部分: document.getElementById("Snd_Cont_AddrLnk_BG").v
我将如何在 javascript 中转义斜杠// var j = /^(ht|f)tp(s?)://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$;/ 最佳答案 使用 \ 进行转
在解析到这样的对象之前,我要转义 & 和 =: var obb = parseJSON('{"' + text.replace(/&/g, "\",\"").replace(/=/g,"\":\"")
我正在使用 freemarker 生成一个 freemarker 模板。但我需要一些方法来转义 freemarker 标签。 我将如何逃脱 标签或 ${expression} ? 最佳答案 您也可以使
我正在尝试匹配方括号,即 excel 中正则表达式 VBA 中的 []。我正在尝试使用以下代码,但它不起作用。 Public Function IsSpecial(s As String) As L
我通过设置将 PowerShell 添加到我的上下文菜单中: Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\she
我需要转义 $,因此我需要将所有出现的 $ 替换为 \$ 所以我写了这个方法: // String#replaceAll(String regex, String replacement) publi
我正在格式化我的问题。非常遗憾。这是我的问题的摘要 在 JSP 中我有一个字段 我输入的值类似于“cQN==ujyRMdr+Qi8dO9Xm*eRun+ner==aLTyt?aKmGI” 实际行动
我有一个文本文件,其内容是C:\temp 我想要值 C:\temp替换为从变量定义的不同值 此外,将从批处理文件(windows .cmd)中调用 perl oneliner set CMDDIR=C
有没有办法使用 jTemplates 来转义 {$,这样我就可以在 onBlur 中使用内联 javascript,例如 telegraaf 在 processTemplate 之后得到这个: 谢谢
我正在尝试将 wget 与包含“#”符号的 url 一起使用。无论我做什么来逃避这个角色,它都不起作用。我用过\、' 和 "。但它们都不起作用。有人有什么建议吗? 谢谢! 最佳答案 如果您真的想让它有
我想知道如何从数据库中回显带有 $ 符号的字符串。此时,数据库中的值“Buy one for $5.00”将转换为“Buy one for .00”。 假设该字段的名称为 title,值为 Buy o
我在 mySQL 中有一个查询,旨在返回我们网站上使用的搜索词。是的,这是一个标签云,是的,我知道它是一条鲻鱼 :) 我们有一个管理页面,管理员可以在其中查看搜索词并选择将它们排除在云端之外。这些词进
我有一个文本区域。在其点击事件上。我将其插入数据库中,然后将其显示为元素列表中的第一个元素。问题是。如果我输入""在textarea中,jquery无法正确显示。它显示为空。代码是 var note
我想知道是否有某种字符串前缀,这样 cstring 就可以按原样使用,而不需要我转义所有字符。我不是 100% 确定。我记得一些关于在字符串前加上 @ 符号( char str[] = @"some\
这个问题在这里已经有了答案: How do I escape curly-brace ({}) characters in a string while using .format (or an f
C/C++编译器如何操作源代码中的转义字符["\"]?如何编写用于处理该字符的编译器语法?遇到那个字符后,编译器会做什么? 最佳答案 大多数编译器分为几个部分:编译器前端称为 lexical anal
我计划接受用户输入,并将其插入到一个 div 中 user_content 一个用户提供内容,另一个用户接收内容。 我认为我会遵循的建议来自 https://www.owasp.org/index.p
我有一个这种形式的 url - http:\\/\\/en.wikipedia.org\\/wiki\\/The_Truman_Show。我怎样才能使它成为正常的网址。我试过使用 urllib.unq
我有一个带有转义数据的字符串 escaped_data = '\\x50\\x51' print escaped_data # gives '\x50\x51' 什么 Python 函数会对其进行反转
我是一名优秀的程序员,十分优秀!