如何检索JSON对象的特定值

时间:2019-07-09 14:18:50

标签: android json kotlin retrofit2

在下面的代码中,我正在发出HTTP请求,以从特定的URL中获取一些数据。我想获取对象“ continue”的结果,这在下面的JSON部分中显示。 我创建了对象模型,如下代码所示。但是,尽管它们包含值,但我从“偏移”和“继续”获得的结果却为空。

我通过以下方式调查了网址 JSON Validate

链接:     JSONResponse

活动

class MainActivity : AppCompatActivity(){

val LOG_TAG : String = this::class.java.simpleName
var mEditTextSearch : EditText? = null
var mButtonCheckResult : Button? = null
var mTextView : TextView? = null

//as comm. channel between backend and frontend
val WikiApiServe by lazy {
    WikiApiService.create();
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    initViews()
}

private fun beginSearch(srsearch : String) {
    WikiApiServe.hitCountCheck("query", "json","search", srsearch)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
            {result ->
                Log.e(LOG_TAG, "error.message: " + result.query.searchinfo.totalhits)
            },
            {error -> Log.e(LOG_TAG, "error.message: " + error.message)}
        )

    WikiApiServe.hitContinueAsSrOffsetInt("query", "json","search", srsearch)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
            {result ->
                Log.e(LOG_TAG, "error.message: " + result.sroffset)
            },
            {error -> Log.e(LOG_TAG, "error.message: " + error.message)}
        )


    WikiApiServe.hitContinueAsContinueString("query", "json","search", srsearch)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
            {result ->
                Log.e(LOG_TAG, "error.message: " + result.cont)
            },
            {error -> Log.e(LOG_TAG, "error.message: " + error.message)}
        )
}


fun initViews() : Unit {
    mEditTextSearch = findViewById(R.id.etSearchEntry)
    mButtonCheckResult = findViewById(R.id.btnCheckSearchResult)
    mTextView = findViewById(R.id.tvResult)
}

public fun clickHandler(v : View?) : Unit {

    when(v?.id) {
        R.id.btnCheckSearchResult -> {
            beginSearch("Melania Trump")
        }
    }
}
}

objectModel

object Model {
data class ResultOfQuery(val query: Query)
data class Query(val searchinfo: SearchInfo)
data class SearchInfo(val totalhits: Int)

data class ResultOfContinueAsSrOffsetInt(val sroffset: Sroffset)
data class Sroffset(val sroffset: Int)
data class ResultOfContinueAsContinueString(val cont: Continue)
data class Continue(val cont: String)

JSON

{     batchcomplete:“”,     继续:{         sroffset:10,         继续:“-||”     },     查询:{         搜索信息:{}     } }

结果

2019-07-09 16:09:50.037 15818-15818/com.example.retrofitkotlin_v10 D/MainActivity: result.query.searchinfo.totalhits: 26505
2019-07-09 16:09:55.332 15818-15818/com.example.retrofitkotlin_v10 D/MainActivity: result.sroffset: null
2019-07-09 16:09:55.386 15818-15818/com.example.retrofitkotlin_v10 D/MainActivity: result.cont: null

0 个答案:

没有答案