简体   繁体   English

Android解析JSON多个数组

[英]Android parsing JSON multiple arrays

[{"name":"Mark","Surname":"Gaux"}]
[{"Job":"2","Type":"Office"}]

I have this JSON echoed by a PHP file on the server and then received by my Android application. 我已将此JSON由服务器上的PHP文件回显,然后由我的Android应用程序接收。

Since I didn't manage to merge the two into one array [ ], you notice that I have two arrays [] []. 由于我没有设法将两者合并为一个数组[],因此您会注意到我有两个数组[] []。

When I had one array I parsed it in my Android application using this loop: 当我有一个数组时,我使用以下循环在Android应用程序中对其进行了解析:

try{

    JSONArray jArray = new JSONArray(response);

            for(int i=0;i<jArray.length();i++){
                JSONObject json_data = jArray.getJSONObject(i);
                jname = json_data.getString("name");
                jsurname = json_data.getString("Surname");
...
}

Just a simple example. 只是一个简单的例子。

I'm a bit confused concerning JSONArray and JSONObject. 我对JSONArray和JSONObject感到有些困惑。 JSONArray is anything between [] and JSONObject is anything between {} ? JSONArray在[]和JSONObject之间是什么,在{}之间是什么?

Well now that I have two arrays, how do I loop from one into the other? 好了,现在我有两个数组,如何从一个数组循环到另一个数组?

Or am I better off merging them into one array from the PHP and using the technique I used up to now? 还是我最好将它们从PHP合并到一个数组中,并使用到目前为止使用的技术?

If you're trying to send both arrays in one response you could do that: 如果您尝试在一个响应中发送两个数组,则可以执行以下操作:

[                                         
    [{"name":"Mark","Surname":"Gaux"}],
    [{"Job":"2","Type":"Office"}]
]

Whole JSON string is a JSONArray : 整个JSON字符串是一个JSONArray
getJSONArray(0) contains JSONObject s with name and Surname getJSONArray(0)包含nameSurname JSONObject
getJSONArray(1) contains JSONObject s with Job and Type getJSONArray(1)包含具有JobType JSONObject

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM