상세 컨텐츠

본문 제목

asp.net에서 JSON으로 데이터 받아오기

Programming/asp.net

by luckey 2010. 6. 29. 13:34

본문

asp.net 에서 response.write 형식으로 작성된 내용을 JSON 형태로 받아오기

        string returnData = "[";
        DataSet ds = new DataSet();
        ds = siteManagerDB.STATMONTH(yy, mm);
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            DataRow rowTable = ds.Tables[0].Rows[i];
            returnData += "{\"day\":" + i.ToString() + ", \"cnt1\":" + rowTable["dayCnt"].ToString() + ", \"cnt2\":" + rowTable["dayNum1"].ToString() + " }";
            if (i < ds.Tables[0].Rows.Count - 1)
            {
                returnData += ",";
            }
            else
            {
                returnData += "]";
            }
        }

        Response.ContentType = "text/json";
        Response.Clear();

        Response.Write(returnData);

        Response.Flush();
        Response.End();

위와 같은 형태로 작업을 하면 결과값이 아래와 같이 출력되며 필요한 곳에서 $.getJSON() 으로 불러다 사용할 수 있다.

[{"day":0, "cnt1":7, "cnt2":0 },{"day":1, "cnt1":2, "cnt2":2 },{"day":2, "cnt1":4, "cnt2":2 },{"day":3, "cnt1":2, "cnt2":1 },{"day":4, "cnt1":0, "cnt2":0 },{"day":5, "cnt1":1, "cnt2":0 },{"day":6, "cnt1":1, "cnt2":0 },{"day":7, "cnt1":3, "cnt2":1 },{"day":8, "cnt1":7, "cnt2":0 },{"day":9, "cnt1":7, "cnt2":2 },{"day":10, "cnt1":12, "cnt2":0 },{"day":11, "cnt1":3, "cnt2":1 },{"day":12, "cnt1":8, "cnt2":0 },{"day":13, "cnt1":29, "cnt2":2 },{"day":14, "cnt1":18, "cnt2":1 },{"day":15, "cnt1":9, "cnt2":3 },{"day":16, "cnt1":21, "cnt2":2 },{"day":17, "cnt1":18, "cnt2":1 },{"day":18, "cnt1":3, "cnt2":0 },{"day":19, "cnt1":5, "cnt2":0 },{"day":20, "cnt1":18, "cnt2":3 },{"day":21, "cnt1":16, "cnt2":3 },{"day":22, "cnt1":11, "cnt2":1 },{"day":23, "cnt1":17, "cnt2":2 },{"day":24, "cnt1":20, "cnt2":5 },{"day":25, "cnt1":5, "cnt2":0 },{"day":26, "cnt1":8, "cnt2":0 },{"day":27, "cnt1":18, "cnt2":1 },{"day":28, "cnt1":8, "cnt2":1 },{"day":29, "cnt1":0, "cnt2":0 }]

참고URL : http://kr.blog.yahoo.com/jooonho/147

관련글 더보기

댓글 영역