如何在Android Java中创建此JSON对象:

huangapple go评论82阅读模式
英文:

how to make this json object in android java

问题

这是我的代码:

{
    "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 API Gateway。请帮助我在Android Studio的Java中创建这个对象。谢谢。

英文:

Here is my code:

{
    "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]]}"
}

I want to send above json to aws apigateway via POST request. Please help me to make this object in android studio java.
Thanks.

答案1

得分: -1

尝试一下

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();
}
英文:

Try this:

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

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

        JSONObject jsonObject=new JSONObject();

        try {
            jsonObject.put(&quot;data&quot;,jsonArray);

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

            Log.i(&quot;FinalJson&quot;,object.toString());

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

huangapple
  • 本文由 发表于 2020年10月27日 16:04:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64550288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定