使用gsonforJava的JSON解析我想解析JSON中的数据,它的类型是String..我在用谷歌gson.我有:jsonLine = "{
"data": {
"translations": [
{
"translatedText": "Hello world"
}
]
}}";我的课是:public class JsonParsing{
public void parse(String jsonLine) {
// there I would like to get String "Hello world"
}}
3 回答
ABOUTYOU
TA贡献1812条经验 获得超5个赞
这是简单的代码,我避免了所有的检查,但这是主要的想法。
public String parse(String jsonLine) { JsonElement jelement = new JsonParser().parse(jsonLine); JsonObject jobject = jelement.getAsJsonObject(); jobject = jobject.getAsJsonObject("data"); JsonArray jarray = jobject.getAsJsonArray("translations"); jobject = jarray.get(0).getAsJsonObject(); String result = jobject.get("translatedText").getAsString(); return result;}
千巷猫影
TA贡献1829条经验 获得超7个赞
Map jsonJavaRootObject = new Gson().fromJson("{/*whatever your mega complex object*/}", Map.class)
Map<String, Object> javaRootMapObject = new Gson().fromJson(jsonLine, Map.class); System.out.println( ( (Map) ( (List) ( (Map) ( javaRootMapObject.get("data") ) ).get("translations") ).get(0) ).get("translatedText") );
小怪兽爱吃肉
TA贡献1852条经验 获得超1个赞
public class Wrapper { public Data data;}static class Data { public Translation[] translations;}static class Translation { public String translatedText;}
Wrapper value = GSON.fromJSON(jsonString, Wrapper.class);String text = value.data.translations[0].translatedText;
添加回答
举报
0/150
提交
取消