개발은 재밌어야 한다
article thumbnail
반응형

안녕하세요

뭔가 오랜만에 글을 작성하는 것 같은데 최근에 토이프로젝트를 진행하고 코딩테스트에 대한 공부를 하느라 블로그에는 신경을 쓰지 못하고 있었는데요.

최근에 방문자수가 늘고 있는게 보이더라구요...

근데 항상 느끼던게 티스토리의 방문 통계 차트 그래프는 항상 느리더라구요... 그래서 차트 데이터도 한번 볼 겸 

오늘은 소소하게 차트데이터를 통해서 최근 방문자수의 총 합을 한번 알아보도록 하겠습니다!

티스토리의 관리 페이지를 처음 띄웠을때의 화면입니다

이 차트의 데이터는 구성을 한번 보도록 하겠습니다.

일단 /manage로 진입했을때에 호출하는것들은 count.json, trend.json, toEntry.json등 이렇게 있습니다.

한번 찬찬히 response데이터를 먼저 보겠습니다

count.json

lastUpdateTime, result, timestamp이렇게 있는데 아마 lastUpdateTime 마지막에 업데이트한 시간이고 result에는 오늘방문자수, 어제방문자수, 총 방문자수가 담겨잇는것 같고 timestamp는 오늘 날짜의 0시0분0초 이렇게 담겨있는것같은데 데 타임스탬프의 역할은 잘 모르겠네요. 아마 어제와 오늘을 구분하기 위해서 넣지 않을까 싶은데 굳이 필요한지는 의문이긴합니다.

trend.json

다음으로는 trend.json인데요

아마 이게 제가 원하는 데이터가 아닐까 싶습니다 

날짜별로 각 count가 담겨있는데 각 날짜별로 방문자 수가 count로 나타내는 데이터 형태로 구성되어있습니다.

topEntry.json

이건 최근 7일 인기글 통계에 나타내는 데이터인것 같습니다

 

자 그럼 제가 원하는 trend.json을 살펴보도록 하겠습니다.

response로 가서 데이터를 복사해봅니다

http://json.parser.online.fr/

 

Json Parser Online

 

json.parser.online.fr

여기서 JSON 데이터를 깔끔하게 보여줍니다 

데이터를 보니 제가 원하는 데이터 포맷이 맞는것 같습니다.

이번에는 코드로 이 JSON 데이터의 값들중에 count를 모두 더해서 총합이 얼마나 되는지 한번 알아보도록 하겠습니다.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
 
import java.util.Map;
 
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
 
public class VisitCounter {
    public static void main(String[] args) {
        // 방문자수 JSON 데이터
                String jsonData = "{\"status\":\"ok\",\"data\":[{\"timestamp\":\"2021-07-15T00:00:00.000\",\"count\":29},{\"timestamp\":\"2021-07-16T00:00:00.000\",\"count\":3},{\"timestamp\":\"2021-07-17T00:00:00.000\",\"count\":1},{\"timestamp\":\"2021-07-18T00:00:00.000\",\"count\":1},{\"timestamp\":\"2021-07-19T00:00:00.000\",\"count\":15},{\"timestamp\":\"2021-07-20T00:00:00.000\",\"count\":28},{\"timestamp\":\"2021-07-21T00:00:00.000\",\"count\":8},{\"timestamp\":\"2021-07-22T00:00:00.000\",\"count\":11},{\"timestamp\":\"2021-07-23T00:00:00.000\",\"count\":14},{\"timestamp\":\"2021-07-24T00:00:00.000\",\"count\":9},{\"timestamp\":\"2021-07-25T00:00:00.000\",\"count\":2},{\"timestamp\":\"2021-07-26T00:00:00.000\",\"count\":20},{\"timestamp\":\"2021-07-27T00:00:00.000\",\"count\":18},{\"timestamp\":\"2021-07-28T00:00:00.000\",\"count\":4},{\"timestamp\":\"2021-07-29T00:00:00.000\",\"count\":13},{\"timestamp\":\"2021-07-30T00:00:00.000\",\"count\":12},{\"timestamp\":\"2021-07-31T00:00:00.000\",\"count\":2},{\"timestamp\":\"2021-08-01T00:00:00.000\",\"count\":18},{\"timestamp\":\"2021-08-02T00:00:00.000\",\"count\":13},{\"timestamp\":\"2021-08-03T00:00:00.000\",\"count\":23},{\"timestamp\":\"2021-08-04T00:00:00.000\",\"count\":6},{\"timestamp\":\"2021-08-05T00:00:00.000\",\"count\":13},{\"timestamp\":\"2021-08-06T00:00:00.000\",\"count\":9},{\"timestamp\":\"2021-08-07T00:00:00.000\",\"count\":22},{\"timestamp\":\"2021-08-08T00:00:00.000\",\"count\":8},{\"timestamp\":\"2021-08-09T00:00:00.000\",\"count\":66},{\"timestamp\":\"2021-08-10T00:00:00.000\",\"count\":16},{\"timestamp\":\"2021-08-11T00:00:00.000\",\"count\":37},{\"timestamp\":\"2021-08-12T00:00:00.000\",\"count\":6},{\"timestamp\":\"2021-08-13T00:00:00.000\",\"count\":21}]}";
 
                /*
                 * { "status":"ok", "data":[ { "timestamp":"2021-07-15T00:00:00.000", "count":29
                 * }, { "timestamp":"2021-07-16T00:00:00.000", "count":3 }, {
                 * "timestamp":"2021-07-17T00:00:00.000", "count":1 }, {
                 * "timestamp":"2021-07-18T00:00:00.000", "count":1 }, {
                 * "timestamp":"2021-07-19T00:00:00.000", "count":15 }, {
                 * "timestamp":"2021-07-20T00:00:00.000", "count":28 }, {
                 * "timestamp":"2021-07-21T00:00:00.000", "count":8 }, {
                 * "timestamp":"2021-07-22T00:00:00.000", "count":11 }, {
                 * "timestamp":"2021-07-23T00:00:00.000", "count":14 }, {
                 * "timestamp":"2021-07-24T00:00:00.000", "count":9 }, {
                 * "timestamp":"2021-07-25T00:00:00.000", "count":2 }, {
                 * "timestamp":"2021-07-26T00:00:00.000", "count":20 }, {
                 * "timestamp":"2021-07-27T00:00:00.000", "count":18 }, {
                 * "timestamp":"2021-07-28T00:00:00.000", "count":4 }, {
                 * "timestamp":"2021-07-29T00:00:00.000", "count":13 }, {
                 * "timestamp":"2021-07-30T00:00:00.000", "count":12 }, {
                 * "timestamp":"2021-07-31T00:00:00.000", "count":2 }, {
                 * "timestamp":"2021-08-01T00:00:00.000", "count":18 }, {
                 * "timestamp":"2021-08-02T00:00:00.000", "count":13 }, {
                 * "timestamp":"2021-08-03T00:00:00.000", "count":23 }, {
                 * "timestamp":"2021-08-04T00:00:00.000", "count":6 }, {
                 * "timestamp":"2021-08-05T00:00:00.000", "count":13 }, {
                 * "timestamp":"2021-08-06T00:00:00.000", "count":9 }, {
                 * "timestamp":"2021-08-07T00:00:00.000", "count":22 }, {
                 * "timestamp":"2021-08-08T00:00:00.000", "count":8 }, {
                 * "timestamp":"2021-08-09T00:00:00.000", "count":66 }, {
                 * "timestamp":"2021-08-10T00:00:00.000", "count":16 }, {
                 * "timestamp":"2021-08-11T00:00:00.000", "count":37 }, {
                 * "timestamp":"2021-08-12T00:00:00.000", "count":6 }, {
                 * "timestamp":"2021-08-13T00:00:00.000", "count":21 } ] }
                 */
 
                try {
                    JSONParser json = new JSONParser();
                    
                    //JSON parse로 데이터를 넣어 JSON Object로 만들어 준다 string->jsonObject
                    JSONObject jsonObject = (JSONObject)json.parse(jsonData); 
                    
                    // status와 data가 있는데 data를 추출
                    JSONArray dataArray = (JSONArray)jsonObject.get("data");
                    
                    // 데이터 확인용 출력
                    for (Object data : dataArray)
                    {
                        System.out.println(data);
                    }
                    
                    int totalVisit = 0;
                    
                    for (Object data: dataArray)
                    {
                        // countMap으로 담아줘서 Map형태로 만들어준다.
                        
                        Map<Object, Object> countMap = (Map<Object, Object>) data;
                        // 카운트만 뽑아서 출력
                        System.out.println(countMap.get("count"));
                        // 방문자수 더하기
                        totalVisit += Integer.parseInt(countMap.get("count").toString());    // toString으로 String으로 변환후 int값으로 변환
                    }
                    System.out.println("총 방문자수는 ==>>>" + totalVisit);
                    
                } catch (Exception e) {
                    // TODO: handle exception
                }
    }
}
cs

 

출력

{"count":29,"timestamp":"2021-07-15T00:00:00.000"}
{"count":3,"timestamp":"2021-07-16T00:00:00.000"}
{"count":1,"timestamp":"2021-07-17T00:00:00.000"}
{"count":1,"timestamp":"2021-07-18T00:00:00.000"}
{"count":15,"timestamp":"2021-07-19T00:00:00.000"}
{"count":28,"timestamp":"2021-07-20T00:00:00.000"}
{"count":8,"timestamp":"2021-07-21T00:00:00.000"}
{"count":11,"timestamp":"2021-07-22T00:00:00.000"}
{"count":14,"timestamp":"2021-07-23T00:00:00.000"}
{"count":9,"timestamp":"2021-07-24T00:00:00.000"}
{"count":2,"timestamp":"2021-07-25T00:00:00.000"}
{"count":20,"timestamp":"2021-07-26T00:00:00.000"}
{"count":18,"timestamp":"2021-07-27T00:00:00.000"}
{"count":4,"timestamp":"2021-07-28T00:00:00.000"}
{"count":13,"timestamp":"2021-07-29T00:00:00.000"}
{"count":12,"timestamp":"2021-07-30T00:00:00.000"}
{"count":2,"timestamp":"2021-07-31T00:00:00.000"}
{"count":18,"timestamp":"2021-08-01T00:00:00.000"}
{"count":13,"timestamp":"2021-08-02T00:00:00.000"}
{"count":23,"timestamp":"2021-08-03T00:00:00.000"}
{"count":6,"timestamp":"2021-08-04T00:00:00.000"}
{"count":13,"timestamp":"2021-08-05T00:00:00.000"}
{"count":9,"timestamp":"2021-08-06T00:00:00.000"}
{"count":22,"timestamp":"2021-08-07T00:00:00.000"}
{"count":8,"timestamp":"2021-08-08T00:00:00.000"}
{"count":66,"timestamp":"2021-08-09T00:00:00.000"}
{"count":16,"timestamp":"2021-08-10T00:00:00.000"}
{"count":37,"timestamp":"2021-08-11T00:00:00.000"}
{"count":6,"timestamp":"2021-08-12T00:00:00.000"}
{"count":21,"timestamp":"2021-08-13T00:00:00.000"}
29
3
1
1
15
28
8
11
14
9
2
20
18
4
13
12
2
18
13
23
6
13
9
22
8
66
16
37
6
21
총 방문자수는 ==>>>448

네 차트에서 보이는 총 방문자수의 합은 448입니다.!!!

 

 

최근 저의 차트에서 보이는 방문자수 448에 감사를 표하면서 포스팅 마치겠습니다.

 

 

 

 

 

<참고. JSON 호출에 대한 데이터의 값들은 저의 추측이므로 정확한 데이터의 의미가 아닐 수 있습니다.>

반응형
profile

개발은 재밌어야 한다

@ghyeong

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!