Responding with a JSON object in Node.js (converting object/array to JSON string)

前端 未结 6 1092
广开言路
广开言路 2020-12-04 08:01

I\'m a newb to back-end code and I\'m trying to create a function that will respond to me a JSON string. I currently have this from an example

function rando         


        
6条回答
  •  心在旅途
    2020-12-04 08:41

    You have to use the JSON.stringify() function included with the V8 engine that node uses.

    var objToJson = { ... };
    response.write(JSON.stringify(objToJson));
    

    Edit: As far as I know, IANA has officially registered a MIME type for JSON as application/json in RFC4627. It is also is listed in the Internet Media Type list here.

提交回复
热议问题