Programming/JAVA / / 2019. 11. 30. 18:49

java json 파싱(gson) 및 수정

반응형

JSON 파일

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
  "company": "tistory",
  "address": "seoul",
  "number": "0212345678",
  "newEmployees": [
    {
      "name": "kim",
      "age": "29",
      "isNew": true
    },
    {
      "name": "park",
      "age": "27",
      "isNew": true
    },
    {
      "name": "lee",
      "age": "26",
      "isNew": true
    }
  ]
}
cs

1. Gson jar 파일 lib 추가

  • gson-2.3.1.jar 

2. json 파일 읽기

  • JsonElement element = jsonParser.parse(new FileReader("C:\\Users\\USER\\Desktop\\java-workspaces\\JavaTest\\jsontestfile\\gson.json"));

3. Object 만들기

  • jsonobject = element.getAsJsonObject();

4. jsonarray 넣기

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package json;
 
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
 
public class JsonReadTest {
    public static void main(String[] args) {
        FileWriter writer=null;
        try {
 
            JsonObject jsonobject = new JsonObject();
            JsonParser jsonParser = new JsonParser();
            JsonElement element = jsonParser.parse(new FileReader("C:\\Users\\USER\\Desktop\\java-workspaces\\JavaTest\\jsontestfile\\gson.txt"));
            jsonobject = element.getAsJsonObject();
            JsonArray memberArray = (JsonArray) jsonobject.get("newEmployees");
            JsonObject object = (JsonObject) memberArray.get(1);
            System.out.println(Integer.parseInt(object.get("age").toString().replace("\"""")));
            String aaa = object.get("age").toString();
            System.out.println(aaa);
            object.addProperty("park""bang");
            System.out.println(object.get("name"));
            System.out.println(memberArray);
            
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            String json = gson.toJson(element);
            try {
                writer = new FileWriter("C:\\Users\\USER\\Desktop\\java-workspaces\\JSONTest\\jsontestfile\\new_gson.json");
                writer.write(json);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                try {
                    writer.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
 
        }
    }
}
 
cs
반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유