
- Python (使用 Flask 框架)cztaihubay.com.cn
python
from flask import Flask, jsonify
app = Flask(name)
假设的游戏商品列表
games = [
{“id”: 1, “name”: “Call of Duty”, “price”: 59.99},
{“id”: 2, “name”: “The Witcher 3”, “price”: 39.99},
{“id”: 3, “name”: “Minecraft”, “price”: 26.95}
]
@app.route(‘/games’, methods=[‘GET’])
def get_games():
return jsonify(games)
if name == ‘main’:
app.run(debug=True)
2. JavaScript (Node.js + Express)
javascript
const express = require(‘express’);
const app = express();
const port = 3000;
// 假设的游戏商品列表
let games = [
{id: 1, name: ‘Call of Duty’, price: 59.99},
{id: 2, name: ‘The Witcher 3’, price: 39.99},
{id: 3, name: ‘Minecraft’, price: 26.95}
];
app.get(‘/games’, (req, res) => {
res.json(games);
});
app.listen(port, () => {
console.log(Game Store listening at http://localhost:${port});
});
3. Java (使用 Spring Boot)
由于 Java 代码较长且需要构建环境,这里只给出核心 Controller 的简化示例。
java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@RestController
public class GameController {
private static final List<Map<String, Object>> games = Arrays.asList(
Map.of("id", 1, "name", "Call of Duty", "price", 59.99),
Map.of("id", 2, "name", "The Witcher 3", "price", 39.99),
Map.of("id", 3, "name", "Minecraft", "price", 26.95)
);
@GetMapping("/games")
public List<Map<String, Object>> getGames() {
return games;
}
}
4. C# (ASP.NET Core)
csharp
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
[Route(“[controller]”)]
[ApiController]
public class GamesController : ControllerBase
{
private readonly List _games = new List
{
new Game { Id = 1, Name = “Call of Duty”, Price = 59.99 },
new Game { Id = 2, Name = “The Witcher 3”, Price = 39.99 },
new Game { Id = 3, Name = “Minecraft”, Price = 26.95 }
};
[HttpGet]
public IActionResult GetGames()
{
return Ok(_games);
}
public class Game
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}
这些示例提供了构建游戏商城API的基础框架。每个示例都展示了如何从服务器返回一组游戏商品数据。在实际应用中,你还需要添加用户认证、支付处理、数据库集成等功能。
946

被折叠的 条评论
为什么被折叠?



