【问题标题】:Parse json in Java (netbean) [duplicate]在Java(netbean)中解析json [重复]
【发布时间】:2017-06-30 09:44:22
【问题描述】:

我知道这个问题已经被问过很多次了,但是当我在java中解析json时我找不到一个好的解决方案。 例如服务器返回一个字符串 {“密钥”:“9c4c”} 我如何解析这种字符串,虽然这可能不是标准的 Json。 有人可以帮忙吗?谢谢

【问题讨论】:

  • 你试过什么?我会在一秒钟内发布一些代码
  • 你知道它已经被问过很多次了,所以想必你已经找到了解决方案。为什么您发现的解决方案不够好?您发布的那一点 JSON 没有什么不标准的。
  • 我已经阅读了其他帖子,但是这些帖子中的 json 是不同的。我是json新手,我的json格式是{"key":"abc123"},比如这个格式。而且我也不知道要导入哪个jar。

标签: java json parsing


【解决方案1】:

这是一种方法,代码注释解释:

public class JSONParser {
    String url;
    String requestMethod;
    public JSONParser(String url, String requestMethod){
        this.url = url;
        this.requestMethod = requestMethod;
    }
    private String read(BufferedReader bufferedReader) throws IOException {
        //Creates new StringBuilder to avoid escaping chars

        StringBuilder stringBuilder = new StringBuilder();
        //Creates new variable

        String currentLine;
        //Gets the currentLine

        while((currentLine = bufferedReader.readLine()) !=null ){

            //Adds the currentLine to the stringBuild if the currentLine is not null

            stringBuilder.append(currentLine);
        }
        //Returns the StringBuilder is String format

        return stringBuilder.toString();
    }

    public JSONObject sendRequest() throws IOException, JSONException {
        //Get the URL from the constructor

        URL url = new URL(this.url);
        //Opens the connection

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        //Sets the requestMethod

        connection.setRequestMethod(this.requestMethod);
        //Sets the requestProperties

        connection.setRequestProperty("Content-type", "application/JSON");
        //Tries to read the through the BufferedReader

        try {
            //Creates new BufferedReader & InputStreamReader which get the input of the connection

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            //Stores the output of the connection in a string

            String jsonText = read(bufferedReader);
            //Creates a new JSON object from the above String

            JSONObject json = new JSONObject(jsonText);
            //Returns the JSON Object

            return json;
        } finally {
            //Disconnects from the URL
            connection.disconnect();
        }
    }

    public void storeJSONData(){
        try{
            //Sends request
            JSONObject json = sendRequest();
            //Gets the JSON array, after that the first JSON object
            json.getJSONArray("").getJSONObject(0).getString("Key");
        }
        catch (IOException e){
            System.out.print(e);
        }
        catch (JSONException e){
            System.out.print(e);
        }


    }
}

【讨论】:

  • 非常感谢您的热心回答和帮助,Sander。但是当我遵循您的代码时遇到问题:org.json.JSONException: JSONObject[""] not found。请问您将如何打印键值以及您使用哪个.jar?
猜你喜欢
  • 2011-06-28
  • 2017-10-04
  • 1970-01-01
  • 2012-02-14
  • 1970-01-01
  • 1970-01-01
  • 2013-11-02
  • 1970-01-01
  • 2017-06-13
相关资源
最近更新 更多