-1

A 有来自 Json 的硬字符串。例子:

[
    {
        "Group1": [
            {
                "id": "2b3b0db",
                "name": "Ivan"
            },
            {
                "id": "4f3b0db",
                "name": "Lera"
            }
        ]
    },
    {
        "Group2": [
            {
                "id": "42ae2a7",
                "name": "Victor"
            }
        ]
    }
]

我如何从 Gson 解析它?谢谢!

4

4 回答 4

2

此链接描述了使用 Gson 的 json to java 和 java to json

http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/


使用 GSON 的 Json 到 Java

Gson gson = new Gson();
try {

    BufferedReader br = new StringReader(<CONTENT>);

    //convert the json string back to object
    // In your case this is Group object
    Object obj = gson.fromJson(br, Object .class);

    System.out.println(obj);

} catch (IOException e) {
    e.printStackTrace();
}


使用 GSON 的 Java 对象到 json 字符串

Object obj = new Object();
Gson gson = new Gson();

// convert java object to JSON format,
// and returned as JSON formatted string
String json = gson.toJson(obj);

在你的情况下使用这个:

public static void main(final String[] args) {
    String Group1 = " [ { \"id\": \"2b3b0db\", \"name\": \"Ivan\" }, { \"id\": \"4f3b0db\", \"name\": \"Lera\" } ] ";

    Gson gson = new Gson();
    Group[] object = gson.fromJson(new StringReader(Group1), Group[].class);
    System.out.println(object);
}

public class Group {

    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public void setId(final String id) {
        this.id = id;
    }
}
于 2013-07-03T04:07:49.610 回答
1

尝试以下代码

   public String parse(String jsonLine)
{
   JSONArray jArraymain=new JSONArray(jsonLine);


    JSONObject  jobject=array.getJSONObject(0);

       JSONArray jArraySub=jobject.getJSONArray("Group1");


 for(int i=0;i<=jobject.length;i++)
  {


   String temp=jArraySub.get(i).toString();

   }




} 



also you can use this 

 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").toString();
return result;

}

于 2013-07-03T05:45:28.663 回答
0

你必须使用GSON吗?如果你可以使用来自json.org的 JSONObject,你几乎可以得到它:

    URL url = this.getClass().getClassLoader().getResource("json" + File.separator + "GroupJson.json");
    JSONArray jsonArray = new JSONArray(FileUtil.readFile(url.getPath()));
    JSONObject jsonObject = jsonArray.getJSONObject(0);
    log.info("{}", jsonObject.toString());

这输出: {"Group1":[{"id":"2b3b0db","name":"Ivan"},{"id":"4f3b0db","name":"Lera"}]}

于 2013-07-03T18:43:41.357 回答
0

下面将为您提供帮助。我还没有执行下面的代码片段。

JsonElement jelement = new JsonParser().parse("json data");
JsonArray  jgrouparray = jelement.getAsJsonArray();
for(int i=0; i<jgrouparray.size(); i++){
    JsonArray jgroup = jgrouparray.get(i).getAsJsonArray("Group"+i);
    for(int j=0; j<jgroup.size(); j++){
        JsonObject jobject = jgroup.get(j).getAsJsonObject();
        String id = jobject.get("id").toString();
        String name = jobject.get("name").toString();
    }
}

有关更多信息,请查看:JSON parsing using Gson for Java

于 2013-07-03T04:21:59.033 回答