json文件:
{
"news":[
{"id":1,"title":"男人看了沉默,女人看了流泪","author":"yiyi"},
{"id":2,"title":"震惊!他年薪仅1元","author":"linlin"}
],
"comments":[
{"id":1,"body":"我是男人","newsId":1},
{"id":2,"body":"我是女人","newsId":1},
{"id":3,"body":"我年薪2元","newsId":2},
{"id":4,"body":"我年薪3元","newsId":2}
]
}
思路:
- json文件里的news和comments通过id和newsId进行连接;
- id为1的author:yiyi发布了两篇文章:就是newsId为1的两篇文章;
- 所以代码是先在news里面找到author为yiyi的人,再通过news.id=comments.newsId的关系找到他发布的两篇文章“我是男人”、“我是女人”;
代码: