Expressjs response as JSON and Xml

后端 未结 2 1125
眼角桃花
眼角桃花 2021-01-06 09:27

What i am doing:: I am trying to generate json and xml output for a dataset from database

Express Code:: Here i am trying for JSON response

相关标签:
2条回答
  • 2021-01-06 09:34

    Those <0> tags are there because you are embedding an array into 'restaurants'. Instead of that, try to name every restaurant as an object array:

    ...

    response.send(xml({restaurants:[
        name_of_restaurants.map(function(r){
            return { restaurant_name? :
                [ 
                    { Buf_Type_Id: r.Buf_Type_Id },
                    { Buf_Type_Name: r.Buf_Type_Name }
                ]
        })
    ]}));
    

    ...

    0 讨论(0)
  • 2021-01-06 09:50

    So I've looked around for a better object-to-XML mapper for you. I tried three before I found one that I liked (it's easy-to-use, and it makes sense for your application). Ditch the old one, and use object-to-xml instead:

    var o2x = require('object-to-xml');
    
    response.set('Content-Type', 'text/xml');
    response.send(o2x({
        '?xml version="1.0" encoding="utf-8"?' : null,
        restaurants: {
            restaurant: name_of_restaurants
        }
    }));
    
    0 讨论(0)
提交回复
热议问题