2015-10-18 92 views
2

我想要找到一种标准的方式来将JSON内容转换为JSON,同时应用转换,类似于XSLT对XML的做法。令我惊讶的是,找不到任何标准,只有一半的烘焙实现。 这样做的普遍接受方式是什么?例如,是否可以将JSON转换为XML,应用XSLT,然后再转换回JSON?JSON到JSON转换与.NET

+0

或许jsonpatch可能是一个答案:http://jsonpatch.com/。 –

+0

感谢您的支持! –

+0

我开始在.net中编写一个库,它将操作应用于json文档,并为您提供一个转换后的json(不是真正的jsonpatch,因为我现在使用jsonpath而不是jsonpointer):仍然在dev中:https://github.com/ CedricDumont/CExtensions-Patch –

回答

1

我写了这个,这有可能被移植到.NET没有太多精力& /或可能激发你:使用

https://jsfiddle.net/YSharpLanguage/kj9pk8oz/10

举例来说,以从https://www.w3.org/TR/xslt#section-Document-Example灵感:

/* 
    Cf. https://www.w3.org/TR/xslt#section-Document-Example 
*/ 
var D1document = { 
    type: "document", title: [ "Document Title" ], 
    "": [ 
     { type: "chapter", title: [ "Chapter Title" ], 
     "": [ 
     { type: "section", title: [ "Section Title" ], 
      "": [ 
      { type: "para", "": [ "This is a test." ] }, 
      { type: "note", "": [ "This is a note." ] } 
     ] }, 
     { type: "section", title: [ "Another Section Title" ], 
      "": [ 
      { type: "para", "": [ "This is ", { emph: "another" }, " test." ] }, 
      { type: "note", "": [ "This is another note." ] } 
     ] } 
     ] } 
    ] }; 

var D1toHTML = { $: [ 
    [ [ function(node) { return node.type === "document"; } ], 
    function(root) { 
     return "<html>\r\n\ 
    <head>\r\n\ 
    <title>\r\n\ 
     {title}\r\n".of(root) + "\ 
    </title>\r\n\ 
    </head>\r\n\ 
    <body>\r\n\ 
{*}".of(root[""].through(this)) + "\ 
    </body>\r\n\ 
</html>"; 
    } 
    ], 
    [ [ function(node) { return node.type === "chapter"; } ], 
    function(chapter) { 
     return " <h2>{title}</h2>\r\n".of(chapter) + "{*}".of(chapter[""].through(this)); 
    } 
    ], 
    [ [ function(node) { return node.type === "section"; } ], 
    function(section) { 
     return " <h3>{title}</h3>\r\n".of(section) + "{*}".of(section[""].through(this)); 
    } 
    ], 
    [ [ function(node) { return node.type === "para"; } ], 
    function(para) { 
     return " <p>{*}</p>\r\n".of(para[""].through(this)); 
    } 
    ], 
    [ [ function(node) { return node.type === "note"; } ], 
    function(note) { 
     return ' <p class="note"><b>NOTE: </b>{*}</p>\r\n'.of(note[""].through(this)); 
    } 
    ], 
    [ [ function(node) { return node.emph; } ], 
    function(emph) { 
     return "<em>{emph}</em>".of(emph); 
    } 
    ] 
] }; 

console.log(D1document.through(D1toHTML)); 

...这给:

<html> 
    <head> 
    <title> 
     Document Title 
    </title> 
    </head> 
    <body> 
    <h2>Chapter Title</h2> 
    <h3>Section Title</h3> 
    <p>This is a test.</p> 
    <p class="note"><b>NOTE: </b>This is a note.</p> 
    <h3>Another Section Title</h3> 
    <p>This is <em>another</em> test.</p> 
    <p class="note"><b>NOTE: </b>This is another note.</p> 
    </body> 
</html> 

...而以下(比较。 https://jsfiddle.net/YSharpLanguage/ppfmmu15/10

// (A "Company" is just an object with a "Team") 
function Company(obj) { 
    return obj.team && Team(obj.team); 
} 

// (A "Team" is just a non-empty array that contains at least one "Member") 
function Team(obj) { 
    return ({ }.toString.call(obj) === "[object Array]") && 
     obj.length && 
     obj.find(function(item) { return Member(item); }); 
} 

// (A "Member" must have first and last names, and a gender) 
function Member(obj) { 
    return obj.first && obj.last && obj.sex; 
} 

function Dude(obj) { 
    return Member(obj) && (obj.sex === "Male"); 
} 

function Girl(obj) { 
    return Member(obj) && (obj.sex === "Female"); 
} 

var data = { team: [ 
    { first: "John", last: "Smith", sex: "Male" }, 
    { first: "Vaio", last: "Sony" }, 
    { first: "Anna", last: "Smith", sex: "Female" }, 
    { first: "Peter", last: "Olsen", sex: "Male" } 
] }; 

var TO_SOMETHING_ELSE = { $: [ 

    [ [ Company ], 
    function(company) { 
     return { some_virtual_dom: { 
     the_dudes: { ul: company.team.select(Dude).through(this) }, 
     the_grrls: { ul: company.team.select(Girl).through(this) } 
     } } 
    } ], 

    [ [ Member ], 
    function(member) { 
     return { li: "{first} {last} ({sex})".of(member) }; 
    } ] 

] }; 

console.log(JSON.stringify(data.through(TO_SOMETHING_ELSE), null, 4)); 

...会给你:

{ 
    "some_virtual_dom": { 
     "the_dudes": { 
      "ul": [ 
       { 
        "li": "John Smith (Male)" 
       }, 
       { 
        "li": "Peter Olsen (Male)" 
       } 
      ] 
     }, 
     "the_grrls": { 
      "ul": [ 
       { 
        "li": "Anna Smith (Female)" 
       } 
      ] 
     } 
    } 
} 

“希望这有助于