0

我有一个java代码:

URL oracle = new URL("https://x.x.x.x.x.x.-001");
System.out.println(oracle.openStream());
BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));

String inputLine;
while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);

这是打开连接并打印它的内容。内容确实是Json。输出类似于:

{
  "merchantId": "guest",
  "txnId": "guest-1349269250-001",
}

我希望在json simple jar 中解析它。我改变了这样的代码循环:

JSONObject obj = new JSONObject();
while ((inputLine = in.readLine()) != null)
        obj.put("Result",inputLine);

但这似乎不起作用。我得到的输出是:

{"Result":"}"}
4

4 回答 4

3

您应该使用JSONParser#Parse()方法或JSONValue#parse()方法:

URL oracle = new URL("https://x.x.x.x.x.x.-001");
System.out.println(oracle.openStream());
Reader in = new InputStreamReader(oracle.openStream());

Object json = JSONValue.parse(in);
于 2012-10-03T12:47:54.327 回答
2

您确定您正在关注有关如何解析 JSON 字符串的文档吗?

从外观上看,您必须获取整个字符串并JSONParse#parse()对其调用 a ,但是您的代码正在用 JSON 的每一行填充HashMap(JSONObject的父类)。事实上,它只存储最后一行,因为您在每次迭代时都put()使用相同的键进行调用。"Result"

于 2012-10-03T12:36:28.987 回答
1

您应该首先将整个内容读取到 String 变量并将其解析为 json。小心""(双引号)。Java\"用于双引号。像。

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonSimpleExample3 {

    public static void main(String args[]) {

        JSONParser parser = new JSONParser();
        //String str = "{\"merchantId\": \"guest\",\"txnId\": \"guest-1349269250-001\",}";

        //intilize an InputStream
        InputStream is = new ByteArrayInputStream("file content".getBytes());

        //read it with BufferedReader and create string
        BufferedReader br = new BufferedReader(new InputStreamReader(is));// Instead of is, you should use oracle.openStream()
        StringBuilder sb = new StringBuilder();

        String line;
        try {
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        } 

        // parse string
        try {
            JSONObject jsonObject = (JSONObject) parser.parse(sb.toString());

            String merchantId = (String) jsonObject.get("merchantId");
            System.out.println(merchantId);
            String txnId = (String) jsonObject.get("txnId");
            System.out.println(txnId);

        } catch (ParseException e) {

            e.printStackTrace();
        }
    }
}
于 2012-10-03T12:35:30.120 回答
0

如果您要登录或像 Java Json 这样简单的工作人员,请尝试此链接它真的很有帮助

import java.io.IOException;
import java.net.URL;

import org.apache.commons.io.IOUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;

public class ParseJson1 {

public static void main(String[] args) {
    String url = "http://freemusicarchive.org/api/get/genres.json?api_key=60BLHNQCAOUFPIBZ&limit=2";
    /*
     * {"title":"Free Music Archive - Genres","message":"","errors":[],"total" : "161","total_pages":81,"page":1,"limit":"2",
     * "dataset":
     * [{"genre_id": "1","genre_parent_id":"38","genre_title":"Avant-Garde" ,"genre_handle": "Avant-Garde","genre_color":"#006666"},
     * {"genre_id":"2","genre_parent_id" :null,"genre_title":"International","genre_handle":"International","genre_color":"#CC3300"}]}
     */
    try {
        String genreJson = IOUtils.toString(new URL(url));
        JSONObject genreJsonObject = (JSONObject) JSONValue.parseWithException(genreJson);
        // get the title
        System.out.println(genreJsonObject.get("title"));
        // get the data
        JSONArray genreArray = (JSONArray) genreJsonObject.get("dataset");
        // get the first genre
        JSONObject firstGenre = (JSONObject) genreArray.get(0);
        System.out.println(firstGenre.get("genre_title"));
    } catch (IOException | ParseException e) {
        e.printStackTrace();
    }
}

}

于 2015-07-04T19:32:19.263 回答