-2

A have a hard string from Json. Example:

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

How i can parse it from Gson? Thanks!

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
zimper
  • 109
  • 2

4 Answers4

2

This link describes json to java and java to json using Gson

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


Json to Java using GSON

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();
}


Java Object to json String using GSON

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);

In your case use this :

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;
    }
}
Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
  • Thanks!. But how i can get one object in string? Example: String Group1 = [ { "id": "2b3b0db", "name": "Ivan" }, { "id": "4f3b0db", "name": "Lera" } ] – zimper Jul 03 '13 at 04:22
  • Can you please explain a little bit more what actully you want to be done. Json string to java Object or java Object to json String? – Ashish Aggarwal Jul 03 '13 at 04:29
  • Sorry! I wanna to place in my String first Group. "Group1": [ { "id": "2b3b0db", "name": "Ivan" }, { "id": "4f3b0db", "name": "Lera" } ] – zimper Jul 03 '13 at 04:32
  • that means you have java objects and you want to be parse then into json string i.e. Group1": [ { "id": "2b3b0db", "name": "Ivan" }, { "id": "4f3b0db", "name": "Lera" } ] . So for that use second code mentioned in my ans – Ashish Aggarwal Jul 03 '13 at 04:34
  • Object obj = gson.fromJson(result, Object.class); String s = obj.toString(); But my string = all objects. But i need only first or second object. How i can do it? Sorry. – zimper Jul 03 '13 at 04:43
  • Check my ans again I update code hope it will work for you – Ashish Aggarwal Jul 03 '13 at 04:46
  • I understand! Object[] obj = gson.fromJson(result, Object[].class); Thanks! Thanks! – zimper Jul 03 '13 at 04:47
1

try following code

   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;

}

Shyam
  • 6,376
  • 1
  • 24
  • 38
0

Below will help you. I havnt executed the below code snippet yet.

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();
    }
}

For more info look into: JSON parsing using Gson for Java

Community
  • 1
  • 1
Dinesh
  • 414
  • 1
  • 6
  • 13
  • thanks! but "The method getAsJsonArray() in the type JsonElement is not applicable for the arguments (String)". Why? – zimper Jul 03 '13 at 04:27
0

Do you have to use GSON? If you can use JSONObject from json.org you can almost get it:

    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());

this outputs: {"Group1":[{"id":"2b3b0db","name":"Ivan"},{"id":"4f3b0db","name":"Lera"}]}

Alper Akture
  • 2,445
  • 1
  • 30
  • 44