我使用org.json库作为我的 Java 应用程序的 JSON 客户端,我想知道这个库中某些方法的复杂性。
我正在通过它的 HTTP API 从数据库中检索另一个 JSON 对象(等等)内的 JSON 数组中的数千个 JSON 对象。作为一个例子(仅作为一个例子,我的情况要复杂得多),假设我正在做这样的事情:
// Ignoring attributes types
import org.json.*;
public static void main(String[] args) {
response = MyHTTPClient.post(url, query).asJSON();
response = JSON.parse(response);
data = response.getJSONObject(1).getJSONArray("results").getJSONObject(0);
}
Run Code Online (Sandbox Code Playgroud)
org.json库的复杂性getJSONObject(int)
和getJSONArray(String)
方法是什么?它是在恒定 [O(1)] 或线性 [O(n)] 时间内运行吗?如果没有,正确答案是什么?
我的项目使用Spring Boot 2.0.4。我正在尝试从文件中读取 XML,然后将其转换为 JSON。这曾经有效,但最近它停止工作并抛出以下异常;
java.lang.NoSuchMethodError: org.json.JSONTokener.<init>(Ljava/io/Reader;)V
at org.json.XMLTokener.<init>(XMLTokener.java:57) ~[json-20180813.jar:na]
at org.json.XML.toJSONObject(XML.java:516) ~[json-20180813.jar:na]
at org.json.XML.toJSONObject(XML.java:548) ~[json-20180813.jar:na]
at org.json.XML.toJSONObject(XML.java:472) ~[json-20180813.jar:na]
at com.zf.trw.visualisation.parser.handler.AttritionHandler.extractLineData(AttritionHandler.java:32) ~[classes/:na]
at com.zf.trw.visualisation.parser.handler.HandlerImp.processFile(HandlerImp.java:79) ~[classes/:na]
at com.zf.trw.visualisation.shared.service.ParserService.manuallyProcessAttritionData(ParserService.java:85) ~[classes/:na]
at com.zf.trw.visualisation.parser.component.ScheduledTask.processAttritionDataFilesForAllLines(ScheduledTask.java:49) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_171]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171]
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) [spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_171]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_171]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_171]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_171]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_171]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_171]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_171]
Run Code Online (Sandbox Code Playgroud)
我用来将 XML 转换为 JSON 的代码如下;
String …
Run Code Online (Sandbox Code Playgroud) 我试图JSONObject
在Eclipse中使用Java应用程序.我到处搜索各个论坛.我还没找到合适的答案.而且,他们一直提到一个WEB-INF/lib directory
我无法找到的.
我尝试json-lib-2.2.2-jdk15.jar
在项目.jar文件中添加.这似乎也没有用.它显示了这个错误 -
导入org.json.JSONObject无法解析
我怎么解决这个问题?
我想从(http://maps.googleapis.com/maps/api/geocode/json?address=public+library+san+diego&sensor=false)从地理编码API在我的Android应用程序接收的JSON响应解析地址.
任何人都可以帮我解决如何解析响应并在列表视图中显示它?
非常感谢您的帮助.
我在服务器端制作JSON字符串.
JSONObject responseObject = new JSONObject();
List<JSONObject> authorList = new LinkedList<JSONObject>();
try {
for (Author author : list) {
JSONObject jsonAuthor = new JSONObject();
jsonAuthor.put("name", author.getName());
jsonAuthor.put("surname", author.getSurname());
authorList.add(jsonAuthor);
}
responseObject.put("authors", authorList);
} catch (JSONException ex) {
ex.printStackTrace();
}
return responseObject.toString();
Run Code Online (Sandbox Code Playgroud)
这就是我在客户端部分解析该字符串的方式.
List<Author> auList = new ArrayList<Author>();
JSONValue value = JSONParser.parse(json);
JSONObject authorObject = value.isObject();
JSONArray authorArray = authorObject.get("authors").isArray();
if (authorArray != null) {
for (int i = 0; i < authorArray.size(); i++) {
JSONObject authorObj = authorArray.get(i).isObject();
Author author …
Run Code Online (Sandbox Code Playgroud) 我正在使用 org.json 解析器。我正在使用 .json 获取一个 json 数组jsonObject.getJSONArray(key)
。问题是jsonArray.length()
返回我1
并且我的 json 数组有 2 个元素,我做错了什么?
String key= "contextResponses";
JSONObject jsonObject = new JSONObject(jsonInput);
Object value = jsonObject.get("contextResponses");
if (value instanceof JSONArray){
JSONArray jsonArray = (JSONArray) jsonObject.getJSONArray(key);
System.out.println("array length is: "+jsonArray.length());/*the result is 1! */
}
Run Code Online (Sandbox Code Playgroud)
这是我的json:
{
"contextResponses" : [
{
"contextElement" : {
"type" : "ENTITY",
"isPattern" : "false",
"id" : "ENTITY3",
"attributes" : [
{
"name" : "ATTR1",
"type" : "float",
"value" : ""
}
] …
Run Code Online (Sandbox Code Playgroud) 我有这样的代码,值jArrAnswer
是
[{"answer":"Yes"},{"answer":"No"},{"answer":"maybe"},{"answer":"yrg"}]
Run Code Online (Sandbox Code Playgroud)
结果jArrAnswer.length()
是4
但为什么我得到错误
org.json.JSONException:索引1超出范围[0..1).
try {
JSONArray jArrAnswerid = new JSONArray(answerid);
JSONArray jArrAnswer = new JSONArray(answer);
for (int i = 0; i < jArrAnswer.length(); i++) {
JSONObject jObjAnswerid = jArrAnswerid.getJSONObject(i);
JSONObject jObjAnswer = jArrAnswer.getJSONObject(i);
String ansid = jObjAnswerid.getString("answerid");
String ans= jObjAnswer.getString("answer");
GroupModel item2 = new GroupModel(String.valueOf(i + 1), ans, ansid);
}
} catch (Exception e) {
Log.w("asdf", e.toString());
}
Run Code Online (Sandbox Code Playgroud)