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

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

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

JSONP(JSON with padding)は、JSONを使用した関数呼び出しのための仕組み。クロスドメインでのデータの受け渡しが可能です。JavaScriptからクロスドメインで容易にデータを扱うことができます。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

2回答

1233閲覧

「Kotlin」データクラスから任意の文字列を取得したいと考えております。

tami6

総合スコア2

JSONP

JSONP(JSON with padding)は、JSONを使用した関数呼び出しのための仕組み。クロスドメインでのデータの受け渡しが可能です。JavaScriptからクロスドメインで容易にデータを扱うことができます。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2022/02/03 15:46

前提・実現したいこと

データクラスから任意の文字列を取得したいと考えております。
できればfor文で{words.length}まで繰り返し処理をして、”text”を配列に格納していき、最終的には文字列として表示させたいと考えております。しかし実装方法が分からず苦戦しております。

発生している問題・エラーメッセージ

var text =result.regions.lines.word.get(0).text
linesが赤字のエラーになっています
エラーメッセージ:Function invocation 'lines(...)' expected
原因は分かっているのですが、解決方法が分かりません。

JSON

{ "language": "ja", "textAngle": 0.0, "orientation": "Up", "regions": [ { "boundingBox": "181,56,1235,2696", "lines": [ { "boundingBox": "560,368,304,2256", "words": [ { "boundingBox": "568,368,296,376", "text": "A" }, { "boundingBox": "592,2448,184,176", "text": "B" } ] }, { "boundingBox": "1001,1518,86,1232", "words": [ { "boundingBox": "1041,1518,35,43", "text": "C" }, { "boundingBox": "1001,2648,33,102", "text": "D" } ] } ] } ], "modelVersion": "2021-04-01" }

データクラス

**Example.java** @Generated("jsonschema2pojo") public class Example { @SerializedName("language") @Expose public String language; @SerializedName("textAngle") @Expose public Float textAngle; @SerializedName("orientation") @Expose public String orientation; @SerializedName("regions") @Expose public List<Region> regions = null; @SerializedName("modelVersion") @Expose public String modelVersion; }
**Line.java** @Generated("jsonschema2pojo") public class Line { @SerializedName("boundingBox") @Expose public String boundingBox; @SerializedName("words") @Expose public List<Word> words = null; }
**Region.java** @Generated("jsonschema2pojo") public class Region { @SerializedName("boundingBox") @Expose public String boundingBox; @SerializedName("lines") @Expose public List<Line> lines = null; }
@Generated("jsonschema2pojo") public class Word { @SerializedName("boundingBox") @Expose public String boundingBox; @SerializedName("text") @Expose public String text; }

該当ソースコード

val result = Gson().fromJson(result, Example::class.java) var text =result.regions.lines.word.get(0).text //このコードに不備があります txt_result.text = text

ご協力よろしくお願いいたします。

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

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

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

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

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

guest

回答2

0

ExampleクラスのregionsはList<Region>を返すので、それに対してRegionの持っているlinesは呼び出せないというのがそのエラーが起きている原因です。

エラーをなくすだけなら方法はいろいろあります.今の使い方であれば先頭の要素だけ取り出してそれに対してlinesを呼び出せば済みます。一般的にはflatMapを使うことになるんじゃないでしょうか。

ちなみにそこを解決してもまったく同じことがwordの呼び出しでも起きますが、それも同様に解決できます。

投稿2022/02/04 01:58

yudedako67

総合スコア2047

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

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

tami6

2022/02/05 08:33

回答ありがとうございます。 参考にさせていただきます。
guest

0

ベストアンサー

Gson().fromJson()

Gsonですか。どうしてもJSONObjectで実装したいのだとおもっていましたが。

データクラスから

「データクラス」をJavaで実装するのではなく、せっかくなら、Kotlinのdata classをつかったらいかがでしょうか。

kotlin

1import com.google.gson.Gson 2import org.junit.Test 3import java.math.BigDecimal 4 5class Hogege { 6 7 @Test 8 fun bb() { 9 val hoge = Gson().fromJson(jsonText, Hoge::class.java) 10 println(hoge) 11 12 val res = 13 hoge.regions 14 .map { region -> 15 region.lines 16 .map { line -> 17 line.words 18 .map { word -> word.text } 19 }.flatten() 20 }.flatten() 21 22 println(res) 23 } 24 25 data class Hoge( 26 val language: String, 27 val textAngle: BigDecimal, 28 val orientation: String, 29 val regions: List<Region> 30 ) 31 32 data class Region( 33 val boundingBox: String, 34 val lines: List<Line> 35 ) 36 37 data class Line( 38 val boundingBox: String, 39 val words: List<Word> 40 ) 41 42 data class Word( 43 val boundingBox: String, 44 val text: String 45 ) 46 47 val jsonText = """ 48 { 49 "language": "ja", 50 "textAngle": 0.0, 51 "orientation": "Up", 52 "regions": [ 53 { 54 "boundingBox": "181,56,1235,2696", 55 "lines": [ 56 { 57 "boundingBox": "560,368,304,2256", 58 "words": [ 59 { 60 "boundingBox": "568,368,296,376", 61 "text": "A" 62 }, 63 { 64 "boundingBox": "592,2448,184,176", 65 "text": "B" 66 } 67 ] 68 }, 69 { 70 "boundingBox": "1001,1518,86,1232", 71 "words": [ 72 { 73 "boundingBox": "1041,1518,35,43", 74 "text": "C" 75 }, 76 { 77 "boundingBox": "1001,2648,33,102", 78 "text": "D" 79 } 80 ] 81 } 82 ] 83 } 84 ], 85 "modelVersion": "2021-04-01" 86 } 87 """.trimIndent() 88}
Hoge(language=ja, textAngle=0.0, orientation=Up, regions=[Region(boundingBox=181,56,1235,2696, lines=[Line(boundingBox=560,368,304,2256, words=[Word(boundingBox=568,368,296,376, text=A), Word(boundingBox=592,2448,184,176, text=B)]), Line(boundingBox=1001,1518,86,1232, words=[Word(boundingBox=1041,1518,35,43, text=C), Word(boundingBox=1001,2648,33,102, text=D)])])]) [A, B, C, D]

投稿2022/02/04 01:21

shiketa

総合スコア3971

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

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

tami6

2022/02/05 08:32

ありがとうございます。任意の文字列を取り出すことに成功いたしました。 kotlin にはdata classというものがあることを知らなかったので、大変助かりましたこちらの方がファイルを作らずに済むので、楽でした。 本当にありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問