[英]Parsing Json object with multiple array and add to a list
I am getting this as a web response from server and i want to parse it and add it to a list 我从服务器获取它作为Web响应,我想解析它并将其添加到列表中
{
"Id": [
"7",
"8",
"9"
],
"Title": [
"Title 1",
"Title 2",
"Title 3"
],
"Description": [
"desc1",
"desc2",
"desc3"
],
"NewsUpdatedDateTime": [
"2010-02-26T10:40:30",
"2010-02-27T10:40:30",
"2010-02-28T10:40:30"
],
"ImageUrl": [
"image1.jpg",
"image2.jpg",
"image3.jpg"
],
"LastUpdatedTime": "2010-02-28T10:40:30",
"ResponseMessage": "Data retrieved successfully",
"ResponseCode": "1"
}
Follow this for JSON Parsing... 按照此进行JSON解析...
I create sample code for your JSON data.. Check and let me know is it working? 我为您的JSON数据创建了示例代码。检查并让我知道它是否有效?
String jsonData = your_data;
ArrayList<String> mListID;
ArrayList<String> mListTitle;
try {
JSONObject jsonOBJ = new JSONObject(jsonData);
// Getting JSON Array node
JSONArray mArrayID = jsonObj.getJSONArray("ID");
mListID = new ArrayList<String>();
for(int i=0 ; i<mArrayId.length(); i++)
{
mListID.add(mArrayId.get(i))
}
JSONArray mArrayTitle = jsonObj.getJSONArray("Title");
mListTitle = new ArrayList<String>();
for(int j=0 ; j<mArrayTitle.length(); j++)
{
mListTitle.add(mArrayTitle.get(j))
}
} catch (JSONException e) {
e.printStackTrace();
}
Try this: 尝试这个:
//your json String
String jsonStr;
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray data = jsonObj.getJSONArray("ID");
} catch (JSONException e) {
e.printStackTrace();
}
}
You should read some tutorials and documentation on json parsing in android from here , here and here . 您应该从此处 , 此处和此处阅读有关android中json解析的一些教程和文档。
You can implement it like this :
List id;
List title;
String jsonTxt = inputStream;
JSONObject jsonObj = new JSONObject(jsonTxt);
JSONArray jsonArray1 = jsonObj.optJSONArray("Id");
JSONArray jsonArray2 = jsonObj.optJSONArray("Title");
if (jsonArray1.length() > 0) {
id=new ArrayList();
for (int index = 0; index < jsonArray1.length(); index++) {
JSONObject sellerObj = jsonArray1.optJSONObject(index);
id.add(sellerObj.optString("name"));//name of ids
}
}
saee as Title or other array. saee作为Title或其他数组。
firstly you parse you server response 首先,您解析服务器响应
json jsonResponse = new json(response);
JSONArray id[] = jsonResponse.getJSONArray("Id");
JSONArray title = jsonResponse.getJSONArray("Title");
JSONArray description[] = jsonResponse.getJSONArray("Description");
JSONArray newsUpdatedDateTime[] = jsonResponse.getJSONArray("NewsUpdatedDateTime");
JSONArray imageUrl[] = jsonResponse.getJSONArray("ImageUrl");
String lastUpdateTime = jsonResponse.getString("LastUpdatedTime");
String responseMessage = jsonResponse.getString("ResponseMessage");
String responseCode = jsonResponse.getString("ResponseCode");
//==========================================================================// // ================================================ ===== //
Then you can parse remain your single array: 然后,您可以解析剩下的单个数组:
for (int i = 0; i < id.length(); i++) { // **line 2**
String name = id.get(i);
}
I think it will help. 我认为这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.