posted by 방랑군 2012. 1. 4. 04:24
출처 :  http://fallove.blog.me/70101916122

최근 이슈가 되고 있는 Json(Javascript Object Notation)은 텍스트 타입의 데이터 처리 방식에 있어서 XML을 대체할 만한 또 다른 형태이다.

장점이라면 XML DOM 호출에 비해 처리 속도가 뛰어나며, Cross-site Domian에서도 사용이 가능하다.

 

현재 관리하고 있는 사이트를 좀 더 가볍게 하기 위해 기존 XML이나 DataSet의 형태를 Json으로 작업하기 위해 관련 자료를 찾아보던 중

Codeplex에서 소개하고 있는 Json.NET framework을 사용해 보았다...

 

        public string Serialize(object value)
        {
            Type type = value.GetType();

            JsonSerializer json = new JsonSerializer();

            json.NullValueHandling = NullValueHandling.Ignore;

            json.ObjectCreationHandling = ObjectCreationHandling.Replace;
            json.MissingMemberHandling = MissingMemberHandling.Ignore;
            json.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

 

            if (type == typeof(DataRow))
                json.Converters.Add(new DataRowConverter());
            else if (type == typeof(DataTable))
                json.Converters.Add(new DataTableConverter());
            else if (type == typeof(DataSet))
                json.Converters.Add(new DataSetConverter());

 

            StringWriter sw = new StringWriter();
            JsonTextWriter writer = new JsonTextWriter(sw);
            if (this.FormatJsonOutput)
                writer.Formatting = Formatting.Indented;
            else
                writer.Formatting = Formatting.None;

 

            writer.QuoteChar = '"';
            json.Serialize(writer, value);

 

            string output = sw.ToString();
            writer.Close();
            sw.Close();

 

            return output;
        }

        public object Deserialize(string jsonText, Type valueType)
        {
            JsonSerializer json = new JsonSerializer();

 

            json.NullValueHandling = NullValueHandling.Ignore;
            json.ObjectCreationHandling = ObjectCreationHandling.Replace;
            json.MissingMemberHandling = MissingMemberHandling.Ignore;
            json.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

 

            StringReader sr = new StringReader(jsonText);
            JsonTextReader reader = new JsonTextReader(sr);
            object result = json.Deserialize(reader, valueType);
            reader.Close();

 

            return result;
        }

 

 

위 두개의 Method는 테이터 타입의 정보를 Json 형태로 변환이 가능하다.

예로 DataSet의 데이터를 Json으로 변환해 보자..

 public void SetJson()

{

    string JsonFileNM = "Test_JsonFile.jsf";

    string JsonString = string.Empty;

    DataSet ds =  null;

 

    ds = Test.GetData(...);

    JsonString = Serialize(ds);

     ....

     //*******************//

     // JsonFileNM로 파일 저장  //

     //*******************//

 ...

}

 

public DataSet GetJson(Sstring jsonText)

{

    DataSet ds = (DataSet)Deserialize(jsonText, typeof(DataSet)));

    return ds;

}

 

Json.NET framework의 특징은 다음과 같다.

-Flexible JSON serializer to convert .NET objects to JSON and back again
-LINQ to JSON for reading and writing JSON
-Writes indented, easy to read JSON
-Convert JSON to and from XML
-Supports Silverlight and Windows Phone

 

CodePlex에서 관련 소스를 download가 가능하다.

http://json.codeplex.com/

http://www.west-wind.com/Weblog/posts/471835.aspx

 
posted by 방랑군 2012. 1. 4. 04:02
jQuery로 Textbox에 값을 가져오고 채우기 위해서는 .val() 함수를 이용한다.


1. 채우기
jQuery("#textbox id").val("abcd");

2. 가져오기
jQuery("#textbox id").val(); 

출처 : http://blog.naver.com/tjjoker?Redirect=Log&logNo=100121652807

ASP.NET 컨트롤 ID제어하기 

============================================================

참고 : * jQuery JavaScript Library v1.4.4
 * http://jquery.com/

내용 : $("[id$='subject']").eq(1)  사용법 인지

 
 $(#"아이디풀네임") 을 적어서 접근을 하지만,

 id$='아이디일부명' 으로 접근이 가능하다.  단 이름이 AA 면  1_AA 등은 가능하지만 AA_ 식으로는 안된다는것이다.

즉, *AA는 되지만 AA* 는 같은 이름으로 인지를 못한다.

일단 같은이름으로 인지가 되면, jQuery은 .eq(인덱스) 를 통해 해당 컨트롤에 접근이 가능하다.

 

 

 

[참고] 컨트롤 존재여부 판단

$("[id$='chk_ItemId']").length 식으로 length 를 하면 없는 객체도 0으로 반환을하므로

존재 여부 판단 가능하다.

======================================================================================

<%@ Page Language="C#" %>

<script src="/jQuery/jquery.js" type="text/javascript"></script>
 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>제목 없음</title>
<script type="text/javascript">
    
    // TEST일 :  2011 - 02 - 09
    // 참고 URL http://cafe.naver.com/jquerymytour.cafe?iframe_url=/ArticleRead.nhn%3Farticleid=64
     function test()
     {     
        //id$='형식으로 적으면' 해당 리스트가 속해있는 이름들을 찾는다. 
        
        //1.서버 텍스트 박스 값 가져오기 
        alert("1. 서버 텍스트 박스 값 >>" +$("[id$='_subject']").val());
        
        //2. 서버 텍스트 박스 값 변경
        $("[id$='_subject']").val("변경"); // 2개다 변경됨 (txt_subject1 변화없음) 
        // _subject2 면 제외댐, 앞은 아무거나 상관없음. 뒤에가 동일한 이름인 모든것을 검색함 ,동일한이름은 배열로저장되어접근가능
        
        //////////////////////////////////////////////////////////////////              
        
        alert("2. 서버 텍스트 박스 값 >>" +$("[id$='_1_TextBox']").val());// _1_TextBox 의값 
        alert("3. 서버 텍스트 박스 값- 배열 1번째값  >>" +$("[id$='_TextBox']").eq(0).val())
        alert("4. 서버 텍스트 박스 값- 배열 2번째값 >>" +$("[id$='_TextBox']").eq(1).val())
        
        //////////////////////////////////////////////////////////////////              
        alert("5. 서버 텍스트 박스 값- 배열 2번째값  >>" +$("[id$='subject']").eq(1).val()) //변경나옴
        alert("6. 서버 텍스트 박스 값- 배열 3번째값>>" +$("[id$='subject']").eq(2).val()) //undefined 나옴 (txt_subject1은 배열로 인식이안됨)
        alert("7. 서버 텍스트 박스 값- 배열 0번째값  >>" +$("[id$='subject']").val() + "=" + $("[id$='subject']").eq(0).val())
        
        var a = $("[id$='subject']");
        alert("배열갯수" + a.length);
        
        // $("#<%=txt_subject.ClientID%>").val();로 작업해도 무방
     }
        </script>
        
</head>
<body>
    <form id="form1" runat="server">

    <asp:Button ID="Button1" OnClientClick="test()"  runat = "server"/>
    s<br />
    <asp:TextBox ID="txt_subject" runat="server"  class="intstyle1" style="width:310px;" ></asp:TextBox>
    <asp:TextBox ID="tx2t_subject" runat="server"  class="intstyle1" style="width:310px;" ></asp:TextBox>
    <asp:TextBox ID="txt_subject1" runat="server"  class="intstyle1" style="width:310px;" Text = "아무변화없음" ></asp:TextBox>
    <br />
    <asp:TextBox ID="_1_TextBox"  runat="server"  class="intstyle1" style="width:310px;" Text = "1" ></asp:TextBox>
    <asp:TextBox ID="_2_TextBox" runat="server"  class="intstyle1" style="width:310px;" Text = "2"></asp:TextBox>
    
    </form>
</body>
</html>

 

'PP > Jquery' 카테고리의 다른 글

[Jquery,CoffeeScript] 모질라 파이어폭스 Ajax. 한글 키 이벤트 처리.  (0) 2012.01.08
jQuery UI 테스트 2  (0) 2012.01.08
jQuery 이벤트 테스트 2  (0) 2012.01.08
jQuery 이벤트 테스트 1  (0) 2012.01.08
jQuery UI 테스트 2  (1) 2012.01.08