java - 如何在 Java 中使用 JSON Simple 创建 JSON

标签 java json

我有以下 json 字符串,我想将其转换为 java 代码,以便我可以更轻松地更改它并发送更新的请求,但我似乎无法弄清楚如何使用 JSONObject 生成它和JSONArray .

["guest_login",{"chips":{"1":2000},"epoch":1552509677,"invested":{"1":2000},"login_id":10,"news":["Welcome to Poker.","Register and get started with 2000 play chips."],"username":"Guest10"}]

我得到以下输出

output = ["guest_login",null,null]

这是我迄今为止尝试过的。

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

    JSONObject obj = new JSONObject();
    JSONArray list = new JSONArray();

    list.add("guest_login");
    list.add(new JSONObject().put("chips", new JSONObject().put("1", "2000")));
    list.add(obj.put("epoch", "1552509677"));
    System.out.println("output = " + list.toString());

也尝试过

        JSONObject json = new JSONObject();
        json.put("name", "student");

        JSONArray array = new JSONArray();
        JSONObject item = new JSONObject();
        item.put("invested", new JSONObject().put("1", "2000"));
        item.put("login_id", "10");         
        item.put("epoch", "1552509677");
        item.put("chips", new JSONObject().put("1", "2000"));
        item.put("news", new JSONArray().add("Welcome to Poker.").add("Register and get started with 2000 play chips."));
        item.put("username","Guest10");
        json.put("guest_login", new JSONArray().add(item));

        message = json.toString();

        System.out.println("output = " + message);

最佳答案

我只是运行此代码并且工作正常,只需更改不同的库:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

...

        JSONObject obj = new JSONObject();
        JSONArray list = new JSONArray();

        list.put("guest_login");
        try {
            list.put(new JSONObject().put("chips", new JSONObject().put("1", "2000")));
            list.put(obj.put("epoch", "1552509677"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

使用下面的代码修复了它,它可以完美地工作!!

        list.put("guest_login");
        try {
            obj.put("chips", new JSONObject().put("1", "2000"));
            obj.put("invested", new JSONObject().put("1", "2000"));
            obj.put("login_id", "10");
            JSONArray news = new JSONArray();
            news.put("Welcome to Poker.");
            news.put("Register and get started with 2000 play chips.");
            obj.put("news", news);   
            obj.put("username","Guest10");
            obj.put("epoch", "119677");
            list.put(obj);
        } catch (JSONException e) {
            e.printStackTrace();
        }

关于java - 如何在 Java 中使用 JSON Simple 创建 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55152583/

相关文章:

java - 无法从绝对路径加载 xml 文件

java - 如何转义 JSON 字符串中日期时间值内的冒号

java - 如何在 JSON/avsc 中标记已弃用的 avro 字段?

Java,父静态方法返回子类型

java - searchview.onQueryTextListener 之后得到错误的位置 recyclerview

json - 解析 Json 显示错误 keyNotFound Swift

sql - 使用嵌套 Json 返回 SQL Server 数据库查询

c# - 带有重复标记的 Json 数组

javascript - 将 json 模式转换为 Angular 树控制树模型

java - 文本转语音 Android Studio