首页 > 解决方案 > 如何在android java中制作这个json对象

问题描述

这是我的代码:

{
    "body": "{\"data\": [[1633209578,0,117.000000],[1633209578,1,116.000000],[1633209624,2,121.000000],[1633209643,3,174.000000],[1633209682,4,222.000000],[1633209715,5,247.000000],[1633209748,6,248.000000],[1633209781,7,248.000000],[1633209814,8,249.000000],[1633209847,9,248.000000],[1633209877,10,248.000000],[1633209912,11,248.000000],[1633209943,12,248.000000],[1633209990,13,248.000000],[1633210009,14,248.000000]]}"
}

我想通过 POST 请求将上面的 json 发送到 aws apigateway。请帮我在 android studio java 中制作这个对象。谢谢。

标签: javaandroidarraysjsonpost

解决方案


尝试这个:

String[] value=new String[]{"[1633209578, 0, 117.000000]",
            "[1633209578, 1, 116.000000]",
            "[1633209624, 2, 121.000000]",
            "[1633209643, 3, 174.000000]",
            "[1633209682, 4, 222.000000]",
            "[1633209715, 5, 247.000000]",
            "[1633209748, 6, 248.000000]",
            "[1633209781, 7, 248.000000]",
            "[1633209814, 8, 249.000000]",
            "[1633209847, 9, 248.000000]",
            "[1633209877, 10, 248.000000]",
            "[1633209912, 11, 248.000000]",
            "[1633209943, 12, 248.000000]",
            "[1633209990, 13, 248.000000]",
            "[1633210009, 14, 248.000000]"};

JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < value.length; i++) {
            jsonArray.put(value[i]);
        }

        JSONObject jsonObject=new JSONObject();

        try {
            jsonObject.put("data",jsonArray);

            JSONObject object=new JSONObject();
            object.put("body",jsonObject);

            Log.i("FinalJson",object.toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }

推荐阅读