質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

1回答

1794閲覧

JsonServerからの応答をJsonObjectに入れたい

kagemaru

総合スコア12

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2019/06/14 09:34

いつもお世話になっております。
前回から引き続きWebサービスの開発をしております。諸々の問題をここで解決させて頂きありがとうございました。
今回は表題の通り、JsonServerを利用してhttpのテストを行っております。
諸般の事情により相手サーバがテストで使用出来ないため、JsonServerを立ててテストを行おうとしているのですが、サーバ側からの応答をJsonObjectへフォーマットをかけようとして以下のエラーがでてしまい、上手く行きません。
JsonObjectは"{"で始まらなくてはいけないのは理解出来るのですが、実際にJsonServerからのレスポンスが"["で始まっているので上手くフォーマットが出来ません。
どうすれば回避出来るのかご教示頂けないでしょうか。
よろしくお願いいたします。

問題のjavaソース(抜粋)

java

1 try { 2 URL url = new URL(pp.getString("AaaURI")); 3 4 con = (HttpURLConnection) url.openConnection(); 5 6 con.setDoOutput(true); 7 con.setRequestMethod("POST"); 8 con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); 9 OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream()); 10 out.write(hh + hb); 11 out.close(); 12 con.connect(); 13 14 // HTTPレスポンスコード 15 int status = con.getResponseCode(); 16 log.setHttpStatusCd(String.valueOf(status)); 17 18 InputStream in = con.getInputStream(); 19 String encoding = con.getContentEncoding(); 20 21 if (null == encoding) { 22 encoding = "UTF-8"; 23 } 24 25 InputStreamReader inReader = new InputStreamReader(in, encoding); 26 BufferedReader bufReader = new BufferedReader(inReader); 27 28 String line = null; 29 30 while ((line = bufReader.readLine()) != null) { 31 // result.append(line); 32 // postedDate += DisassembleString.ResultValue(line, "Date"); 33 if (line != null) { 34 rb = line; 35 } 36 } 37 38 bufReader.close(); 39 inReader.close(); 40 in.close(); 41 42 JSONObject jsonHttpBody = JSONObject.fromObject(rb);←ここでエラー

db.json

json

1{ 2 "posts": [ 3 { 4 "id": 1, 5 "title": "json-server", 6 "author": "typicode" 7 }, 8 ], 9 "comments": [ 10 { 11 "id": 1, 12 "body": "some comment", 13 "postId": 1 14 } 15 ], 16 "profile": { 17 "name": "typicode" 18 } 19}

返って来る値

json

1[ ← これが原因でJsonObjectにいれられない 2 { 3 "id": 1, 4 "title": "json-server", 5 "author": "typicode" 6 } 7]

エラーメッセージ

net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of } at net.sf.json.util.JSONTokener.syntaxError(JSONTokener.java:499) at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:972) at net.sf.json.JSONObject._fromString(JSONObject.java:1201) at net.sf.json.JSONObject.fromObject(JSONObject.java:165) at net.sf.json.JSONObject.fromObject(JSONObject.java:134) at jp.co.aaa.bbb.service.impl.AaaServiceImpl.httpSend(AaaServiceImpl.java:293) at jp.co.aaa.bbb.service.impl.AaaServiceImpl.invoke(AaaServiceImpl.java:73) at jp.co.aaa.bbb.test.AaaServiceTest.test006(AaaServiceTest.java:160) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) jp.co.aaa.common.ServiceException: net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of } at jp.co.aaa.bbb.service.impl.AaaServiceImpl.httpSend(RegisterPaymentServiceImpl.java:373) at jp.co.aaa.bbb.service.impl.AaaServiceImpl.invoke(RegisterPaymentServiceImpl.java:73) at jp.co.aaa.bbb.test.AaaServiceTest.test006(AaaServiceTest.java:160) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) Caused by: net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of } at net.sf.json.util.JSONTokener.syntaxError(JSONTokener.java:499) at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:972) at net.sf.json.JSONObject._fromString(JSONObject.java:1201) at net.sf.json.JSONObject.fromObject(JSONObject.java:165) at net.sf.json.JSONObject.fromObject(JSONObject.java:134) at jp.co.aaa.bbb.service.impl.AaaServiceImpl.httpSend(AaaServiceImpl.java:293) ... 26 more

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

お騒がせしました…単純なコーディングミスでした。

java

1 while ((line = bufReader.readLine()) != null) { 2 // result.append(line); 3 // postedDate += DisassembleString.ResultValue(line, "Date"); 4 if (line != null) { 5 rb = line; ← 変数rbがStringnなので最後の行だけが残る 6 }

正しくは、

java

1 StringBuffer rb = new SrringBuffer(); 2<中略> 3 while ((line = bufReader.readLine()) != null) { 4 // result.append(line); 5 // postedDate += DisassembleString.ResultValue(line, "Date"); 6 if (line != null) { 7 rb.append(line); 8 } 9<中略> 10 JSONObject jsonHttpBody = JSONObject.fromObject(rb.toString());

上記の様にStringBufferに読込んだJsonを入れていくことで解決出来ました。

投稿2019/06/14 09:57

kagemaru

総合スコア12

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問