如何在Java Android中从JSON中检索数据

huangapple 未分类评论46阅读模式
英文:

how to retrieve data from json in java android

问题

我正尝试编辑一个 Android VPN 应用,它有自己的服务器列表。

我有一个包含服务器数据的 JSON,但不知道如何嵌入它。

这是我的 JSON 响应的样式:

[
    [
        "0",
        "PL",
        "Poland",
        "warswaw",
        "1.1.1.1",
        "IKEv2",
        "",
        ""
    ],
    [
        "1",
        "AU",
        "Australia",
        "Sydney ",
        "1.1.1.1",
        "IKEv2",
        "",
        ""
    ],
    ...
]

以下是应用中的代码:

JSONObject json_response = new JSONObject("http://example.com/json.php");
JSONArray jsonArray = json_response.getJSONArray();
for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject json_object = jsonArray.getJSONObject(i);
    ServerArray[i][0] = json_object.getString("id");
    ServerArray[i][1] = json_object.getString("file");
    ServerArray[i][2] = json_object.getString("city");
    ServerArray[i][3] = json_object.getString("country");
    ServerArray[i][4] = json_object.getString("image");
    ServerArray[i][5] = json_object.getString("ip");
    ServerArray[i][6] = json_object.getString("active");
    ServerArray[i][7] = json_object.getString("signal");
    NumServers = NumServers + 1;
}

数组来自旧 API:https://raw.githubusercontent.com/gayanvoice/android-vpn-client-ics-openvpn/images/appdetails.json

如何修改代码以读取我的服务器列表?

英文:

I am trying to edit an android vpn app that it's own server list.

i have a json of my own with server data but don't know how to embed it.

This is how my json response looks

    [
[
&quot;0&quot;,
&quot;PL&quot;,
&quot;Poland&quot;,
&quot;warswaw&quot;,
&quot;1.1.1.1&quot;,
&quot;IKEv2&quot;,
&quot;&quot;,
&quot;&quot;
],
[
&quot;1&quot;,
&quot;AU&quot;,
&quot;Australia&quot;,
&quot;Sydney &quot;,
&quot;1.1.1.1&quot;,
&quot;IKEv2&quot;,
&quot;&quot;,
&quot;&quot;
],

and this is the code in app

 JSONObject json_response = new JSONObject(&quot;http://example.com/json.php&quot;);
                JSONArray jsonArray = json_response.getJSONArray();
                for (int i = 0; i &lt; jsonArray.length(); i++) {
                    JSONObject json_object = jsonArray.getJSONObject(i);
                    ServerArray[i][0] = json_object.getString(&quot;id&quot;);
               ;
                    ServerArray[i][1] = json_object.getString(&quot;file&quot;);
                    ServerArray[i][2] = json_object.getString(&quot;city&quot;);
               
                    ServerArray[i][3] = json_object.getString(&quot;country&quot;);
                    ServerArray[i][4] = json_object.getString(&quot;image&quot;);
                    ServerArray[i][5] = json_object.getString(&quot;ip&quot;);
                    ServerArray[i][6] = json_object.getString(&quot;active&quot;);
                    ServerArray[i][7] = json_object.getString(&quot;signal&quot;);
                    NumServers = NumServers + 1;
                }

the arrays are from old api https://raw.githubusercontent.com/gayanvoice/android-vpn-client-ics-openvpn/images/appdetails.json

how can i rewrite so that it reads my server list ?

答案1

得分: 0

尝试使用Gson,这会更容易些。

英文:

Try to use Gson - it would be much easier for you.

答案2

得分: 0

你的JSON响应格式为字符串的数组数组。可能有更好的方式使用键值对来创建响应,但这取决于你。

你需要向你的端点发送请求以获取JSON响应。为了解析它,看看你的JSON响应的格式,并按以下方式解析:

JSONArray rootArray = response.getJSONArray(); // 获取根数组
for(int i = 0; i < rootArray.length(); i++){
    JSONArray childArray = rootArray.getJSONArray(i); // 索引i处的子数组
    for(int j = 0; j < childArray.length(); j++){
        // 通过在childArray上使用getString方法遍历并获取字符串
    }
}
英文:

Your JSON response is of the format Array of Arrays of Strings. There are probably better ways to create your response using key-value pairs, but that's up to you.

You have to send a request to your endpoint to get the JSON response. In order to parse it, look at the format of your JSON response, and parse like so:

JSONArray rootArray = response.getJSONArray(); // Gets the root array
for(int i = 0; i &lt; rootArray.length(); i++){
    JSONArray childArray = rootArray.getJSONArray(i); // child array at index i
    for(int j = 0; j &lt; childArray.length(); j++){
    // Traverse through and get strings using getString method on childArray
    }
}

huangapple
  • 本文由 发表于 2020年4月10日 23:45:45
  • 转载请务必保留本文链接:https://java.coder-hub.com/61143782.html
匿名

发表评论

匿名网友

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

确定