Skip to content Skip to sidebar Skip to footer

How To Transfer Escaped Json Lines To List Of Beans

Java POJO: import android.os.Parcel; import android.os.Parcelable; import com.gongzelong.duolingowordsearch.utils.ParcelableUtils; import com.google.gson.annotations.SerializedNa

Solution 1:

Your "JSON" does not look like a JSON. More like list of escaped JSON objects. To deserialise it properly you need to unescape JSON and remove " chars at the beginning and at the end. Also, word_locations / translations is not a list - it is a Map. Let's assume that your payload looks like this:

"{\"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\"}"

Below example app reads this payload line by line, unescapes and deserialises to 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;

publicclassGsonApp {

    publicstaticvoidmain(String[] args) throws Exception {
        File jsonFile = newFile("./resource/test.txt").getAbsoluteFile();

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

        Gson gson = newGsonBuilder().create();

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

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

classWordSearch {

    privateString word;

    @SerializedName("character_grid")
    privateString[][] characterGrid;

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

    @SerializedName("source_language")
    privateString sourceLanguage;

    @SerializedName("target_language")
    privateString targetLanguage;

    publicStringgetWord() {
        return word;
    }

    publicvoidsetWord(String word) {
        this.word = word;
    }

    publicString[][] getCharacterGrid() {
        return characterGrid;
    }

    publicvoidsetCharacterGrid(String[][] characterGrid) {
        this.characterGrid = characterGrid;
    }

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

    publicvoidsetTranslations(Map<String, String> translations) {
        this.translations = translations;
    }

    publicStringgetSourceLanguage() {
        return sourceLanguage;
    }

    publicvoidsetSourceLanguage(String sourceLanguage) {
        this.sourceLanguage = sourceLanguage;
    }

    publicStringgetTargetLanguage() {
        return targetLanguage;
    }

    publicvoidsetTargetLanguage(String targetLanguage) {
        this.targetLanguage = targetLanguage;
    }

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

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

        return builder.toString();
    }
}

Above code prints:

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]
}

See also:

Post a Comment for "How To Transfer Escaped Json Lines To List Of Beans"