简体   繁体   English

从Java中的URL解析多个(数千)JSON对象

[英]Parsing Multiple (Thousands) of JSON objects from a url in Java

I have to deal with multiple thousands of JSON objects that I am getting from a url in Java. 我必须处理从Java网址中获取的成千上万个JSON对象。 I tried using the usual JSON code I have but I get the error Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at 2 [character 3 line 1] 我尝试使用常规的JSON代码,但Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at 2 [character 3 line 1]收到错误Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at 2 [character 3 line 1]

Because there are no curly braces present in the JSON data only square brackets around each JSON object. 由于JSON数据中没有大括号,因此每个JSON对象周围只有方括号。

I would appreciate any help I can get with this. 我将不胜感激。

BTW the code I was trying to use was the code posted in the highest answer here: simplest way to read json from a URL in java 顺便说一句,我试图使用的代码是在最高答案中发布的代码: 从Java中的URL读取json的最简单方法

Thanks 谢谢

I have found out the correct method for handling this. 我已经找到正确的方法来处理此问题。

Once I treat the data as JSON Arrays it works fine. 一旦我将数据视为JSON数组,它就可以正常工作。 I then specify the field I want by number reference rather that name. 然后,我通过数字引用而不是名称来指定我想要的字段。 For example: System.out.println(json.toString()); System.out.println(json.get(2)); 例如: System.out.println(json.toString()); System.out.println(json.get(2)); System.out.println(json.toString()); System.out.println(json.get(2));

will print the header row then print the second field of the header row. 将打印标题行,然后打印标题行的第二个字段。

To process the next object, I presume a loop advancing the line number on each recursion would work fine. 为了处理下一个对象,我假设在每次递归中循环前进行号都可以正常工作。

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

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