How to parse JSON from an url using Kotlin on Android? - android

I've been trying to parse JSON from an url for hours now but I'm too stupid I guess
I have tried
val result = URL("http://date.jsontest.com/").readText()
but it crashes
java.net.MalformedURLException: no protocol: date.jsontest.com
I have tried Klaxon's library
fun parse(name: String) : Any? {
val cls = Parser::class.java
return cls.getResourceAsStream(name)?.let {
inputStream -> return Parser().parse(inputStream)
}
}
val obj = parse("http://date.jsontest.com/") as JsonObject
but it also crashes
kotlin.TypeCastException: null cannot be cast to non-null type com.beust.klaxon.JsonObject
Can someone please write in Kotlin the simpliest way to parse data from this link http://date.jsontest.com/

Hard to say why you are getting an error. Since this line is correct and working.
val result = URL("http://date.jsontest.com/").readText()
For the parse method. It does not work, becase it expects path to a JSON file but is getting a URL String.
Simplest example using Klaxon would be this.
val result = URL("http://date.jsontest.com/").readText()
val parser: Parser = Parser()
val stringBuilder: StringBuilder = StringBuilder(result)
val json: JsonObject = parser.parse(stringBuilder) as JsonObject
println("Time : ${json.string("time")}, Since epoch : ${json.long("milliseconds_since_epoch")}, Date : ${json.string("date")}, ")

Related

Converting an JSON string to Gson object

I have an JSONObject which is creating in the following way.
For a special reason, I have to convert it to Gson in this way, as i can modify only extractDataToString() method. But I do not get proper value after conversion.
fun createJsonString() {
val headerJson = JSONObject()
headerJson.put("typ1", "value1")
headerJson.put("typ2", "value2")
extractData(headerJson.toString())
}
fun extractDataToString(jsonString: String) {
val headerJson = Gson().fromJson(jsonString, JsonObject::class.java)
val resultType1 = headerJson.remove("typ1")?.toString() // Here, resultType1 becomes
"value1", but it should be
value1 that is extra qutomation
mark is being added.
}
Why extra quotation mark is being added? Can anyone please help? I am a newbie.
Call
.getAsString()
Instead of
toString()
at the end
ref : docs

How to add a list or array to a JsonObject as a body of request for volley library

i have a body format request for Restfull api like below
{
"Messages": ["Verification"],
"MobileNumbers": ["09100000000"],
"LineNumber": "300000000000000",
"SendDateTime": "",
"CanContinueInCaseOfError": "false"
}
I create this format by JsonObject as code below
val body : JSONObject = JSONObject()
val messageText = "[\"Verification\"]"
body.put("Messages",messageText )
body.put("MobileNumbers","09120000000" )
body.put("LineNumber","30004005430000")
body.put("SendDateTime","")
body.put("CanContinueInCaseOfError","false")
But I have a problem in creating two first parameters as list and adding them to JsonObject
Can anybody help me for creating exactly like format
Thank you
Finally i find the answer of my Q
I share it for someone who has this problem
The problem solve with LinkedHashMap ... I solved this problem as below
val PhoneNumber = listOf("09120000000")
val messageText3 = listOf("Verification")
val bodymap : LinkedHashMap<String,Any> = LinkedHashMap<String,Any>()
bodymap.put("Messages",messageText3)
bodymap.put("MobileNumbers",PhoneNumber)
val body = JSONObject(bodymap as Map<*, *>)
body.put("LineNumber","30000000000000")
body.put("SendDateTime","")
body.put("CanContinueInCaseOfError","false")
And the output is exactly as the format i wrote in question section

How to parse a JSON string into a JSONObject in Kotlin?

I'm really new in programming, and recently started a project in Kotlin with Android Studio.
So, I have a problem with a JSON object. I get data from an BroadcastReceiver object, a String to be more specific, with the next format:
{"s1":1}
This, is a simple string. So I took in a function call toJson and I do this.
private fun toJson(data:String): JSONObject {
var newData: String = data.replace("\"","")
newData = newData.replace("{","")
newData = newData.replace("}","")
val newObject = newData.split(":")
val name = newObject[0]
val value = newObject[1]
val rootObject = JSONObject()
rootObject.put(name,value)
return rootObject
}
Im doing this the right way?, how can I improve my code?
Thanks for your help, and sorry for my english!
Welcome to StackOverflow!
In 2019 no-one is really parsing JSON manually. It's much easier to use Gson library. It takes as an input your object and spits out JSON string and vice-versa.
Example:
data class MyClass(#SerializedName("s1") val s1: Int)
val myClass: MyClass = Gson().fromJson(data, MyClass::class.java)
val outputJson: String = Gson().toJson(myClass)
This way you're not working with JSON string directly but rather with Kotlin object which is type-safe and more convenient.
Look at the docs. It's pretty big and easy to understand
Here is some tutorials:
https://www.youtube.com/watch?v=f-kcvxYZrB4
http://www.studytrails.com/java/json/java-google-json-introduction/
https://www.tutorialspoint.com/gson/index.htm
UPDATE: If you really want to use JSONObject then use its constructor with a string parameter which parses your JSON string automatically.
val jsonObject = JSONObject(data)
Best way is using kotlinx.serialization. turn a Kotlin object into its JSON representation and back marking its class with the #Serializable annotation, and using the provided encodeToString and decodeFromString<T> extension functions on the Json object:
import kotlinx.serialization.*
import kotlinx.serialization.json.*
​
#Serializable
data class User(val name: String, val yearOfBirth: Int)
​
// Serialization (Kotlin object to JSON string)
​
val data = User("Louis", 1901)
val string = Json.encodeToString(data)
println(string) // {"name":"Louis","yearOfBirth":1901}
​
// Deserialization (JSON string to Kotlin object)
​
val obj = Json.decodeFromString<User>(string)
println(obj) // User(name=Louis, yearOfBirth=1901)
Further examples: https://blog.jetbrains.com/kotlin/2020/10/kotlinx-serialization-1-0-released/
I am adding 3 templates here for Kotlin Developers, It will solve json converting & parsing problems.
//Json Array template
{
"json_id": "12.4",
"json_name": "name of the array",
"json_image": "https://image_path",
"json_description": "Description of the Json Array"
}
Kotlin Model class
data class JsonDataParser(
#SerializedName("json_id") val id: Long,
#SerializedName("json_name") val name: String,
#SerializedName("json_image") val image: String,
#SerializedName("json_description") val description: String
)
Converting to Json String from the Model Class
val gson = Gson()
val json = gson.toJson(jsonDataParser)
Parsing from Json file/Strong
val json = getJson()
val topic = gson.fromJson(json, JsonDataParser::class.java)

Serializer for xml data while calling http post method (Ktor lib)

As I am newbie in Android, I am building an application in which I need to update layer data on Geo server. For that scenario, I m calling post method along with XML body request.
I need to send data in xml format in request body and need to get response in xml. For that, I tried using XmlSerializer instead of JsonFeature but I got error saying "XmlSerializer is not comapanion object, need to initialize here"
val httpClient = HttpClient {
install(JsonFeature) {
serializer = KotlinxSerializer(Json.nonstrict)
}
}
Thanks in Advance!!
There are no XML related features, but you can use existing Json with the XML serializer
The first thing you need to do is to find an appropriate serializer. I used https://github.com/pdvrieze/xmlutil.
implementation("net.devrieze:xmlutil-android:0.20.0.1")
You also can use other platform dependencies(I used the JVM and multiplatform).
Next you can configure the JsonFeature feature with a custom serializer:
val client = HttpClient {
Json {
serializer = XMLSerializer()
accept(ContentType.Application.Xml)
}
}
Using ContentType.Application.Xml
And finally, add the serializer:
#OptIn(ImplicitReflectionSerializer::class)
class XMLSerializer(private val format: XML = XML()) : JsonSerializer {
override fun read(type: TypeInfo, body: Input): Any {
val text = body.readText()
val deserializationStrategy = format.context.getContextual(type.type)
val mapper = deserializationStrategy
?: type.kotlinType?.let { serializer(it) }
?: type.type.serializer()
return format.parse(mapper, text) ?: error("Failed to parse response of type $type. The result is null.")
}
override fun write(data: Any, contentType: ContentType): OutgoingContent {
val serializer = data::class.serializer() as KSerializer<Any>
val text = format.stringify(serializer, data, null)
return TextContent(text, contentType)
}
}
Here is the full result sample with the server(adopted to run without Android): https://gist.github.com/e5l/3b4d5d704b4d7c6e2a65cf68de8e9ca4

Android App crashes as Json element is empty String ("") and not Object

I am working on an android project and using RxAndroid, Retrofit to make API call and retrieve json. The json looks something like following :
{
"result": [
{
"parent": "jhasj",
"u_deviation": "skasks",
"caused_by": "ksks",
"u_owner_mi": {
"link": "https://gddhdd.service-now.com/api/now/v1/table/sys_user/ghtytu",
"value": "ghtytu"
},
"impact": "",
}
]
}
I am using gson to parse the Json. The problem is "u_owner_mi" sometimes reruns empty string "" when there is no value assigned to it. I don't have access to change the return type to null. This is making my app crash as I am expecting an object here.
I get the following error :
Expected BEGIN_OBJECT but was STRING
If you can't modify the server, try replacing the offending line in the server response before passing it to the Gson parser. Something like:
String safeResponse = serverResponse.replace("\"u_owner_mi\": \"\"", "\"u_owner_mi\": null");
Your app (client) code is expecting an object according to a contract specified in the class that you pass to GSON. Your app behaves as it should and crashes loudly. You should consider having your server return "u_owner_mi" : null instead of an empty string, assuming you have control over that. The u_owner_mi field on the client side would have to be a nullable type.
If you don't have the ability to fix the api, you could also write a custom deserializer.
Suppose your result class and sub-object are:
data class Result(
val parent: String,
val owner: Any?
)
data class Owner(
val link: String,
val value: String
)
The deserializer could be:
class ResultDeserializer : JsonDeserializer<Result> {
override fun deserialize(json: JsonElement, typeOfT: Type?, context: JsonDeserializationContext?): Result {
val jsonObject = json.asJsonObject
val ownerProperty = jsonObject.get("owner")
return Result(
parent = jsonObject.get("parent").asString,
owner = if (ownerProperty.isJsonObject) context?.deserialize<Owner>(ownerProperty.asJsonObject, Owner::class.java)
else ownerProperty.asString
)
}
}
Finally, to add the deserializer:
#Test
fun deserialization() {
val gson = GsonBuilder().registerTypeAdapter(Result::class.java, ResultDeserializer()).create()
val result1 = gson.fromJson<Result>(jsonWithObject, Result::class.java)
val result2 = gson.fromJson<Result>(jsonWithEmpty, Result::class.java)
}

Categories

Resources