美文网首页服务端之旅IOS知识整理
模拟服务端数据api JsonServer

模拟服务端数据api JsonServer

作者: 高压锅炖熊 | 来源:发表于2016-04-16 21:50 被阅读2889次

最近公司开发新版本app,但是服务端工作排期已经排到了一个月以后,我们移动端工作还是要先提前做好。我们可以通过服务端提供好的接口文档,自己模拟假数据接口进行开发,测试通过后,等服务端接口弄好,立马就可以调用。
现在我们使用JsonServer来模拟服务端的api

搭建环境(mac)

一、JsonServer环境需要使用Node.js
推荐使用Homebrew安装,有了Homebrew安装软件真是太方便了
1、Homebrew,使用Terminal

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2、Node.js

brew install node 

3、JsonServer

npm install -g json-server 

数据准备

1、先来看下JsonServer提供的json格式文档

{
  "posts": [
    { "id": 1, "title": "json-server", "author": "typicode" }
  ],
  "comments": [
    { "id": 1, "body": "some comment", "postId": 1 }
  ],
  "profile": { "name": "typicode" }
}

2、根据服务端提供的接口文档,JsonServer和制作一份json文件。这里只需将JsonServer提供的格式,对posts里面的数据进行修改就可以了,如下

{
  "posts": 
    {
      "code": 0,
      "msg": "请求成功",
      "result": {
        "events": [
          {
            "title": "新家里快放假啦手指南,教你玩转APP",
            "time": "2016.10.20-2016.12.1",
            "type": "服务",
            "img_url": "http://img.xxxx.cn/Uploads/housephoto/247/246228/56daa00c24a6f.jpg",
            "web_url": "xxx/xxx/xxx.html"
          },
          {
            "title": "放假了咖啡法拉第减肥接入起哦里卡多减肥新手指南,教你玩转APP",
            "time": "2016.10.20-2016.12.1",
            "type": "服务",
            "img_url": "http://xxx/Uploads/housephoto/247/246231/56da9e8460f3b.jpg",
            "web_url": "xx/xxx/xxx.html"
          },
          {
            "title": "新人家去诶节日手指南,教你玩转APP",
            "time": "2016.10.20-2016.12.1",
            "type": "知识",
            "img_url": "http://xxxx/Uploads/housephoto/253/252256/56dc035c55359.JPG",
            "web_url": "xxx/xxx/xxx.html"
          }
        ],
        "page_total": "3"
      }
    }
  ,
  "comments": [
    {
      "id": 1,
      "body": "some comment",
      "postId": 1
    }
  ],
  "profile": {
    "name": "typicode"
  }
}

3、json在线生成网站,网上一搜大把,这里是我随便用的一个,Safiri浏览器貌似是不行的,请使用Chrome或者Firfox浏览器,将生成json文件保存到本地,这里文件保存到桌面Desktop命名为data.json

http://www.qqe2.com

启动JsonServer

1、使用Terminalcd到data.json所在的文件夹

Last login: Sat Apr 16 20:27:29 on ttys003
LIUYONGs-MacBook-Pro:~ liuyong$ ls
77.json     Documents   Movies      Public      import
Applications    Downloads   Music       bin     untitled
Desktop     Library     Pictures    db.json
LIUYONGs-MacBook-Pro:~ liuyong$ cd Desktop/

2、使用Terminal输入json-server --watch data.json,当出现以下内容的时候,表示JsonServer启动,请保证json文件格式正确

LIUYONGs-MacBook-Pro:Desktop liuyong$ json-server --watch db.json

  \{^_^}/ hi!

  Loading data.json
  Done

  Resources
  http://localhost:3000/posts
  http://localhost:3000/comments
  http://localhost:3000/profile

  Home
  http://localhost:3000

  Type s + enter at any time to create a snapshot of the database
  Watching...

查看Json数据(不要关闭Terminal)

1、在浏览器中输入http://localhost:3000/posts

1.png
2、在代码中调用http://localhost:3000/posts
- (void)loadActivityListData {
    [self.dataArray removeAllObjects];
    
    NSString *url = @"http://localhost:3000/posts";
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", nil];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        NSDictionary *dict = responseObject[@"result"];
        NSArray *array = dict[@"events"];
        for (NSDictionary *dict in array) {
            ActivityModel *model = [ActivityModel modelWithDict:dict];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

更多JsonServer使用方法

请登录github查看更多JsonServer的使用方法

相关文章

  • 模拟服务端数据api JsonServer

    最近公司开发新版本app,但是服务端工作排期已经排到了一个月以后,我们移动端工作还是要先提前做好。我们可以通过服务...

  • 创建RESTAPI神器-JsonServer

    JsonServer Get a full fake REST API with zero coding in l...

  • 如何用jsonserver模拟接口获取mock数据

    在做这个解答时,我想说点别的东西。 前后端分离。 到底什么是前后端分离呢?网上有人说,前端是与浏览器打交道的,而后...

  • 模拟数据库服务端 angular-in-memory-web-a

    说明 为了在没有服务端的情况下提前运行和调试angular项目,需要一个构建在内存中的模拟数据库,和对应的api ...

  • react全家桶搭一个博客 - 复盘

    文件目录 api 与 配置文件 与一个入口HTML api里就是mock的json数据,模拟从数据库中取数据。 w...

  • 用nodejs写接口(mock)

    本文实现用nodejs搭建一个简单的服务端,用json文件模拟数据库,适合前端在模拟数据测试的时候使用 必备 no...

  • 用nodejs写接口(mock)

    本文实现用nodejs搭建一个简单的服务端,用json文件模拟数据库,适合前端在模拟数据测试的时候使用 必备 no...

  • 第2章 API文档与模拟数据接口

    第2章 API文档与模拟数据接口 学习目标: 理解RESTful架构 运用Swagger编写API文档 掌握Moc...

  • APP开发模拟API

    测试API的模拟主要分以下2块 1.模拟服务器 2.模拟测试数据 一、模拟服务器解决方案 1).使用Apache开...

  • 2018-03-12

    完善Tcp接收数据的逻辑 workerman与数据库 定制邮件模版,整合后台---先模拟触发 app API开发

网友评论

本文标题:模拟服务端数据api JsonServer

本文链接:https://www.haomeiwen.com/subject/wpholttx.html