gpt4 book ai didi

android studio org.json.JSON.typeMismatch

转载 作者:行者123 更新时间:2023-12-02 12:46:15 25 4
gpt4 key购买 nike

我怎样才能在 Volley 中获得包含 Array 的 JSONObject?

逻辑

one

代码

Note: I'm aware that my api function below works if my result is JSON Array, but I'm not sure how to modify it in order to get JSON Object (as my result is)


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_posts)

callAPIDemo()
}

// api code
private fun callAPIDemo() {
val mySlugValue: String = intent.getStringExtra("my_slug")
// Instantiate the RequestQueue.
val queue = Volley.newRequestQueue(this)
val url = "https://example.com/api/categories/$mySlugValue"

// Request a string response from the provided URL.
val stringRequest = StringRequest(
Request.Method.GET, url,
Response.Listener<String> { response ->
val jsonArray = JSONArray(response)
val list: ArrayList<Post> = ArrayList()
for (i in 0 until jsonArray.length()) {
val jsonObject = jsonArray.getJSONObject(i)
list.add(parseData(jsonObject))
}
// here you will have the complete list of data in your "list" variable
posts_list.layoutManager = LinearLayoutManager(this)
Log.d("my list", list.toString())
posts_list.adapter = MyPostsRecyclerViewAdapter(list)
},
Response.ErrorListener { error ->
//displaying the error in toast if occurrs
Toast.makeText(applicationContext, error.message, Toast.LENGTH_SHORT)
.show()
})
// Add the request to the RequestQueue.
queue.add(stringRequest)
}

// parsing data
private fun parseData(jsonObject: JSONObject): Post {
var listingObject = Post(
jsonObject.getString("name"),
jsonObject.getString("slug"),
jsonObject.getString("image")
)
return listingObject
}

任何的想法?

更新

根据这里的要求,我返​​回的代码如下所示:
{
"id": 10,
"name": "...",
"slug": "...",
"icon": "...",
"body": "...",
"image": "...",
"posts": [
{
"id": 2,
"user": "...",
"name": "...",
"slug": "...",
"image": "...",
"body": "...",
"icon": null,
"quote": null,
"video": null,
"created_at": "2019-11-23 06:05:56",
"updated_at": "2019-11-23 06:53:26"
},
// other posts
],
"created_at": "2019-11-23 05:35:31",
"updated_at": "2019-11-26 11:25:17"
}

最佳答案

你可以像这样解析你的json数据

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_posts)

callAPIDemo()
}

private fun callAPIDemo() {
val mySlugValue: String = intent.getStringExtra("my_slug")
// Instantiate the RequestQueue.
val queue = Volley.newRequestQueue(this)
val url = "https://example.com/api/categories/$mySlugValue"

// Request a string response from the provided URL.
val stringRequest = StringRequest(
Request.Method.GET, url,
Response.Listener<String> { response ->

val list: ArrayList<Post> = ArrayList()
getPosts(response,list)

// here you will have the complete list of data in your "list" variable
posts_list.layoutManager = LinearLayoutManager(this)
Log.d("my list", list.toString())
posts_list.adapter = MyPostsRecyclerViewAdapter(list)
},
Response.ErrorListener { error ->
//displaying the error in toast if occurrs
Toast.makeText(applicationContext, error.message, Toast.LENGTH_SHORT)
.show()
})
// Add the request to the RequestQueue.
queue.add(stringRequest)
}


fun getPosts(response: String,list:ArrayList<Post>) {

var jsonObject = JSONObject(response)
val jsonArray = jsonObject.getJSONArray("posts")

for (i in 0 until jsonArray.length()) {
val jsonObject1 = jsonArray.getJSONObject(i)
var listingObject = Post(
jsonObject1.getInt("id"),
jsonObject1.getString("user"),
jsonObject1.getString("slug"),
jsonObject1.getString("image"),
jsonObject1.getString("body"),
jsonObject1.getString("icon"),
jsonObject1.getString("quote"),
jsonObject1.getString("video"),
jsonObject1.getString("created_at"),
jsonObject1.getString("updated_at")

)
list.add(listingObject)

}
}

你的数据类看起来像这样
data class Post ( val id: Int, val user: String?, val slug: String?,
val image: String?, val body: String?, val icon: String?,
val quote: String?, val video: String?, val created_at: String?,
val updated_at: String?
)

关于android studio org.json.JSON.typeMismatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60142682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com