2016. 5. 25. 12:20

아래 코드를 입력하면 클래스 오브젝트를 XML 또는 JSON 타입으로 리턴시켜준다.

[Global.aspx]

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        // 응용 프로그램 시작 시 실행되는 코드
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        RouteConfig.RegisterRoutes(RouteTable.Routes);

        // Content-type 전역지정
        GlobalConfiguration.Configuration.Formatters.Clear();
        //GlobalConfiguration.Configuration.Formatters.Add(new System.Net.Http.Formatting.XmlMediaTypeFormatter());
        GlobalConfiguration.Configuration.Formatters.Add(new System.Net.Http.Formatting.JsonMediaTypeFormatter());
    }
}


Posted by CoolDragon
2013. 8. 13. 15:41
var express = require('express'),
    app     = express(),
    port    = 8080;

app.configure(function(){
	app.use(express.bodyParser());
});

app.post('/', function(req, res){
	console.log(req.body.);
	res.send({ status: 'SUCCESS' });
})

app.listen(port);


위와 같은 코드로 샘플을 만들고 json을 post data로 넘겨 

서버에서 log로 찍어보려 했지만 아래와 같이 undefined로 나타나더라.. 아니 왜?



한참 삽질하면서 검색하던 중에  content-type이라는 녀석이 눈에 들어온다.. 

그렇다 node.js 의 json data를 서버에 요청할 경우 header에 Content-Type: application/json 을 담아 요청을 했어야 했다.



별것 아닌것 같지만.. 괜한 시간 뺏길 수가 있다.

Posted by CoolDragon
2012. 12. 11. 19:23


JSON 메시지 생성 (출처 : http://code.google.com/p/aspjson/)

JSON_2.0.4.asp

JSON_UTIL_0.1.1.asp

<!-- #include virtual="JSON_2.0.4.asp" -->

<!-- #include virtual="JSON_UTIL_0.1.1.asp" -->

<%

Dim json

Dim person, people(4)


For i = 0 To 4

Set person = jsObject()

person("name") = "park"

person("age") = 20

person("sex") = "male"

Set people(i) = person

Next


Set json = jsObject()

json("count") = Ubound(people)

json("people") = people


Response.Write toJSON(json)

' 결과

{"count":4,"people":[{"name":"park","age":20,"sex":"male"},{"name":"park","age":20,"sex":"male"},{"name":"park","age":20,"sex":"male"},{"name":"park","age":20,"sex":"male"},{"name":"park","age":20,"sex":"male"}]}

%>


JSON 메시지 파싱 (https://github.com/nagaozen/asp-xtreme-evolution/blob/master/lib/axe/classes/Parsers/json2.asp)

json2.asp

<!-- #include virtual="json2.asp" -->


dim Info : set Info = JSON.parse(join(array( _
    "{", _
    " ""firstname"": ""Fabio"",", _
    " ""lastname"": ""長尾"",", _
    " ""alive"": true,", _
    " ""age"": 27,", _
    " ""nickname"": ""nagaozen"",", _
    " ""fruits"": [", _
    " ""banana"",", _
    " ""orange"",", _
    " ""apple"",", _
    " ""papaya"",", _
    " ""pineapple""", _
    " ],", _
    " ""complex"": {", _
    " ""real"": 1,", _
    " ""imaginary"": 2", _
    " }", _
    "}" _
)))

Response.write(Info.firstname & vbNewline) ' prints Fabio
Response.write(Info.alive & vbNewline) ' prints True
Response.write(Info.age & vbNewline) ' prints 27
Response.write(Info.fruits.get(0) & vbNewline) ' prints banana
Response.write(Info.fruits.get(1) & vbNewline) ' prints orange
Response.write(Info.complex.real & vbNewline) ' prints 1
Response.write(Info.complex.imaginary & vbNewline) ' prints 2

Posted by CoolDragon