【问题标题】:Pasring JSON data using Java使用 Java 解析 JSON 数据
【发布时间】:2016-11-17 07:11:54
【问题描述】:

这是我的代码,用于从字符串 json 数据中获取与其关联的属性名称和值。代码执行时没有错误,但我得到的结果为 null。

import java.io.*;
import java.net.*;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

  public class A18 {
public static void main(String[] args) throws ParseException{
String[] out2;
String out,out1= null;
try{
    URL a=new URL("URL");
    HttpURLConnection b=(HttpURLConnection) a.openConnection();
    b.setRequestMethod("GET");
    b.setRequestProperty("Accept", "application/json");
    BufferedReader c=new BufferedReader(new InputStreamReader(b.getInputStream()));
    StringBuilder sb=new StringBuilder();



while((out=c.readLine())!=null)
 {
    sb.append(out);
 out1=sb.toString();
 }

c.close();
b.disconnect();


 JSONObject obj = new JSONObject();
    String id = obj.toJSONString("collection");
    String error = obj.toJSONString("links");
 }
        catch (Exception e)
    {

        e.printStackTrace();
        return;
    }   

    }}

【问题讨论】:

  • 请向我们展示导入语句,因为它们似乎很重要。
  • 检查您的导入。您似乎同时使用了两个 JSON 解析库,并且导入了错误的 JSONObject
  • 我已经解决了,谢谢,但我又遇到了一个错误
  • 您现在遇到了哪个错误?还显示您正在使用的导入语句和提及库。

标签: java arrays json


【解决方案1】:

在您的导入代码中,您拥有import org.json.JSONObject,将其更改为import org.json.simple.JSONObject 应该会有所帮助。

您很可能还有其他 org.json.x 的导入,您必须更改为 org.json.simple.x

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-27
    • 2013-12-09
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 2013-11-15
    • 2022-01-23
    • 2011-07-08
    相关资源
    最近更新 更多