ballerina 学习六 xml && json

ballerina xml && json 参考使用

代码比较简单,使用起来还是比较方便的

xml

代码说明:

import ballerina/io;
function main (string… args) {
xml person1 = xml `<person><fname>John</fname><lname>Doe</lname></person>`;
xml person2 = xml `<person><fname>Jane</fname><lname>Doe</lname></person>`;
xml persons = xml `<persons/>`;
persons.setChildren(person1 + person2);
io:println(persons);
}

输出结果:

<persons>
<person><fname>John</fname><lname>Doe</lname></person>
<person><fname>Jane</fname><lname>Doe</lname></person>
</persons>

json

代码说明:

  • 简单json
import ballerina/io;
function main (string… args) {
json j ={"name":"dalongdemo","age":333};
io:println(j);
}

输出结果:

{"name":"dalongdemo","age":333}
  • 使用自定义类型
import ballerina/http;
import ballerina/io;
type Person {
string name;
int age;
string city;
};
function main (string… args) {
json<Person> person ={name:"dalong"};
person.age=11;
person.city="beijing";
io:println(person);
}

输出结果:

{"name":"dalong","age":11,"city":"beijing"}

参考资料

http://https//ballerina.io/learn/by-example/xml.html
https://ballerina.io/learn/by-example/json.html

posted on 2018-05-18 10:39  荣锋亮  阅读(275)  评论(0编辑  收藏  举报

导航