问题描述
我的问题与下面的问题几乎相同.
My question is almost identical to the one below.
也忘记提及此线程:解析json数组android
我正在解析以下数据. https://api.tfl.gov.uk/Line/Mode/tube% 2Cdlr
I'm parsing the following data. https://api.tfl.gov.uk/Line/Mode/tube%2Cdlr
我只需要提取"id"和"name".就像上面的线程一样,数据被括在方括号中,但没有包含在特定的数组中.我需要解析一个JSON对象,然后在ArrayList中返回数据.我知道如何解析ArrayList.解析时,我是否创建JSON对象或JSON数组?是否可以创建和解析JSONObject,然后将其转换为数组并返回它?
I just need to extract "id" and "name". Like in the thread above, the data is enclosed in square brackets but not in a specific array. I need to parse a JSON object and then return the data in an ArrayList. I know how to parse an ArrayList. When parsing, do I create a JSON object or a JSON Array? Is it possible to create and parse a JSONObject and then convert it to and return it in an array?
在第一个示例中,他们使用HashMap.没有HashMap和GSON可以解析吗?在第二个线程中,他们使用try catch方法从Url中提取数据.我在单独的类中实现了BuildUrl方法.
In the first example they use HashMap. Is it possible to parse it w/o HashMap and GSON? And in the second thread, they extract the data from the Url in the try catch method. I implemented BuildUrl method in a separate class.
P.S.我还没有测试代码.
P.S. I haven't tested the code yet.
JSONUtils类:
JSONUtils class:
public class JSONUtils
{
/**
* Tag for the log messages
*/
private static final String LOG_TAG = JSONUtils.class.getSimpleName();
private static final String KEY_LINE_ID = "id";
private static final String KEY_LINE_NAME = "name";
public JSONUtils()
{
}
public static Lines extractFeatureFromJson (String linesJSON)
{
// If the JSON string is empty or null, then return early.
if (TextUtils.isEmpty(linesJSON)) {
return null;
}
Lines line = null;
try
{
// Create a JSONObject from the JSON file
JSONObject jsonObject = new JSONObject(linesJSON);
String id = "";
if (jsonObject.has("id"))
{
id = jsonObject.optString(KEY_LINE_ID);
}
String name = "";
if (jsonObject.has("name"))
{
name= jsonObject.optString(KEY_LINE_NAME);
}
line = new Lines(id, name);
}
catch (JSONException e)
{
// If an error is thrown when executing any of the above statements in the "try" block,
// catch the exception here, so the app doesn't crash. Print a log message
// with the message from the exception.
Log.e("QueryUtils", "Problem parsing lines JSON results", e);
}
// Return the list of lines
return line;
}
}
推荐答案
您可以尝试以下操作:
您将必须通过您的回复,而我只是将您的所有回复都放在String
中.您可以代表自己传递来自API的响应.
You will have to pass your response and I just put all your response in String
. You can pass your own response from API on that behalf.
String response="[{\"$type\":\"Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities\",\"id\":\"bakerloo\",\"name\":\"Bakerloo\",\"modeName\":\"tube\",\"disruptions\":[],\"created\":\"2018-12-11T17:06:05.303Z\",\"modified\":\"2018-12-11T17:06:05.303Z\",\"lineStatuses\":[],\"routeSections\":[],\"serviceTypes\":[{\"$type\":\"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities\",\"name\":\"Regular\",\"uri\":\"/Line/Route?ids=Bakerloo&serviceTypes=Regular\"}],\"crowding\":{\"$type\":\"Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities\"}},{\"$type\":\"Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities\",\"id\":\"central\",\"name\":\"Central\",\"modeName\":\"tube\",\"disruptions\":[],\"created\":\"2018-12-11T17:06:05.3Z\",\"modified\":\"2018-12-11T17:06:05.3Z\",\"lineStatuses\":[],\"routeSections\":[],\"serviceTypes\":[{\"$type\":\"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities\",\"name\":\"Regular\",\"uri\":\"/Line/Route?ids=Central&serviceTypes=Regular\"},{\"$type\":\"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities\",\"name\":\"Night\",\"uri\":\"/Line/Route?ids=Central&serviceTypes=Night\"}],\"crowding\":{\"$type\":\"Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities\"}},{\"$type\":\"Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities\",\"id\":\"circle\",\"name\":\"Circle\",\"modeName\":\"tube\",\"disruptions\":[],\"created\":\"2018-12-11T17:06:05.3Z\",\"modified\":\"2018-12-11T17:06:05.3Z\",\"lineStatuses\":[],\"routeSections\":[],\"serviceTypes\":[{\"$type\":\"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities\",\"name\":\"Regular\",\"uri\":\"/Line/Route?ids=Circle&serviceTypes=Regular\"}],\"crowding\":{\"$type\":\"Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities\"}},{\"$type\":\"Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities\",\"id\":\"district\",\"name\":\"District\",\"modeName\":\"tube\",\"disruptions\":[],\"created\":\"2018-12-11T17:06:05.3Z\",\"modified\":\"2018-12-11T17:06:05.3Z\",\"lineStatuses\":[],\"routeSections\":[],\"serviceTypes\":[{\"$type\":\"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities\",\"name\":\"Regular\",\"uri\":\"/Line/Route?ids=District&serviceTypes=Regular\"}],\"crowding\":{\"$type\":\"Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities\"}},{\"$type\":\"Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities\",\"id\":\"dlr\",\"name\":\"DLR\",\"modeName\":\"dlr\",\"disruptions\":[],\"created\":\"2018-12-11T17:06:05.303Z\",\"modified\":\"2018-12-11T17:06:05.303Z\",\"lineStatuses\":[],\"routeSections\":[],\"serviceTypes\":[{\"$type\":\"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities\",\"name\":\"Regular\",\"uri\":\"/Line/Route?ids=DLR&serviceTypes=Regular\"}],\"crowding\":{\"$type\":\"Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities\"}},{\"$type\":\"Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities\",\"id\":\"hammersmith-city\",\"name\":\"Hammersmith & City\",\"modeName\":\"tube\",\"disruptions\":[],\"created\":\"2018-12-11T17:06:05.297Z\",\"modified\":\"2018-12-11T17:06:05.297Z\",\"lineStatuses\":[],\"routeSections\":[],\"serviceTypes\":[{\"$type\":\"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities\",\"name\":\"Regular\",\"uri\":\"/Line/Route?ids=Hammersmith & City&serviceTypes=Regular\"}],\"crowding\":{\"$type\":\"Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities\"}},{\"$type\":\"Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities\",\"id\":\"jubilee\",\"name\":\"Jubilee\",\"modeName\":\"tube\",\"disruptions\":[],\"created\":\"2018-12-11T17:06:05.297Z\",\"modified\":\"2018-12-11T17:06:05.297Z\",\"lineStatuses\":[],\"routeSections\":[],\"serviceTypes\":[{\"$type\":\"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities\",\"name\":\"Regular\",\"uri\":\"/Line/Route?ids=Jubilee&serviceTypes=Regular\"},{\"$type\":\"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities\",\"name\":\"Night\",\"uri\":\"/Line/Route?ids=Jubilee&serviceTypes=Night\"}],\"crowding\":{\"$type\":\"Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities\"}},{\"$type\":\"Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities\",\"id\":\"metropolitan\",\"name\":\"Metropolitan\",\"modeName\":\"tube\",\"disruptions\":[],\"created\":\"2018-12-11T17:06:05.297Z\",\"modified\":\"2018-12-11T17:06:05.297Z\",\"lineStatuses\":[],\"routeSections\":[],\"serviceTypes\":[{\"$type\":\"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities\",\"name\":\"Regular\",\"uri\":\"/Line/Route?ids=Metropolitan&serviceTypes=Regular\"}],\"crowding\":{\"$type\":\"Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities\"}},{\"$type\":\"Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities\",\"id\":\"northern\",\"name\":\"Northern\",\"modeName\":\"tube\",\"disruptions\":[],\"created\":\"2018-12-11T17:06:05.3Z\",\"modified\":\"2018-12-11T17:06:05.3Z\",\"lineStatuses\":[],\"routeSections\":[],\"serviceTypes\":[{\"$type\":\"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities\",\"name\":\"Regular\",\"uri\":\"/Line/Route?ids=Northern&serviceTypes=Regular\"},{\"$type\":\"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities\",\"name\":\"Night\",\"uri\":\"/Line/Route?ids=Northern&serviceTypes=Night\"}],\"crowding\":{\"$type\":\"Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities\"}},{\"$type\":\"Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities\",\"id\":\"piccadilly\",\"name\":\"Piccadilly\",\"modeName\":\"tube\",\"disruptions\":[],\"created\":\"2018-12-11T17:06:05.3Z\",\"modified\":\"2018-12-11T17:06:05.3Z\",\"lineStatuses\":[],\"routeSections\":[],\"serviceTypes\":[{\"$type\":\"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities\",\"name\":\"Regular\",\"uri\":\"/Line/Route?ids=Piccadilly&serviceTypes=Regular\"},{\"$type\":\"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities\",\"name\":\"Night\",\"uri\":\"/Line/Route?ids=Piccadilly&serviceTypes=Night\"}],\"crowding\":{\"$type\":\"Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities\"}},{\"$type\":\"Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities\",\"id\":\"victoria\",\"name\":\"Victoria\",\"modeName\":\"tube\",\"disruptions\":[],\"created\":\"2018-12-11T17:06:05.297Z\",\"modified\":\"2018-12-11T17:06:05.297Z\",\"lineStatuses\":[],\"routeSections\":[],\"serviceTypes\":[{\"$type\":\"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities\",\"name\":\"Regular\",\"uri\":\"/Line/Route?ids=Victoria&serviceTypes=Regular\"},{\"$type\":\"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities\",\"name\":\"Night\",\"uri\":\"/Line/Route?ids=Victoria&serviceTypes=Night\"}],\"crowding\":{\"$type\":\"Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities\"}},{\"$type\":\"Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities\",\"id\":\"waterloo-city\",\"name\":\"Waterloo & City\",\"modeName\":\"tube\",\"disruptions\":[],\"created\":\"2018-12-11T17:06:05.297Z\",\"modified\":\"2018-12-11T17:06:05.297Z\",\"lineStatuses\":[],\"routeSections\":[],\"serviceTypes\":[{\"$type\":\"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities\",\"name\":\"Regular\",\"uri\":\"/Line/Route?ids=Waterloo & City&serviceTypes=Regular\"}],\"crowding\":{\"$type\":\"Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities\"}}]\n";
try {
JSONArray jsArray=new JSONArray(response);
for(int i=0;i<jsArray.length();i++)
{
String id=jsArray.getJSONObject(i).getString("id");
String name=jsArray.getJSONObject(i).getString("name");
}
} catch (JSONException ex) {
ex.printStackTrace();
}
它将为您提供id
和name
It will give you id
and name
这篇关于如何在不定义数组的情况下解析JSON数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!