1

クライアントにjson文字列を出力するソケットサーバーを実行しています。それらを解析するために json-simple を使用しようとしました。しかし、私が直面している問題は、サーバーにjson文字列を分離するための区切り文字がないことです。したがって、私の json-simple JSONParser は ParseException をスローします。

代わりに、json-smart を使用してみました。ただし、この場合、JSONParser は最初のオブジェクトのみを返し、残りの文字列は無視します。

私はこのjsonの解析に慣れていません。json 文字列ストリームの正しい処理方法を教えていただければ幸いです。

編集: - JSON 文字列とサンプル コードの追加

{"type":"response","id":"1","result":[true,0]}{"type":"response","id":"2","result":[true,1]}

現在、このメソッドは、json-smart を使用すると単一のオブジェクトを返し、json-simple を使用すると null を返します。

public JSONObject getResponse(JSONObject request) {
    String s = null;
    Socket soc = null;
    PrintWriter sout = null;
    BufferedReader sin = null;
    try {
        soc = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
        sout = new PrintWriter(soc.getOutputStream());
        sin = new BufferedReader(
                new InputStreamReader(soc.getInputStream()));
        sout.println(request.toJSONString());
        sout.flush();
        s = sin.readLine();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            sin.close();
            sout.close();
            soc.close();
        } catch (Exception e) {
        }
    }
    Object response = null;
    try {
        response = JSONValue.parseWithException(s.toString());
    }catch (ParseException e){
        e.printStackTrace();
    }
    return (JSONObject) response;

前もって感謝します、

カジャ

4

2 に答える 2