I know it is possible to save the whole ontology in JSON, would it be possible to convert single OWLClass to JSON?
Something like:
OWLClass cl = ...;
JSONObject obj = cl.toJSON();
Thanks!
There's no ready method to do that in OWLAPI, however an OWLClass consists of just an IRI, whose only data is a string. So it is pretty simple to serialize a single object as a string plus a type specification, and deserialize accordingly.
An introduction to custom serialization for JSON is available here
Related
Wondering if you could help. I am trying to access all the nested first_names from this API inside of elements:
https://fantasy.premierleague.com/api/bootstrap-static/
Here's my controller code:
def index
require 'net/http'
require 'json'
url = 'https://fantasy.premierleague.com/api/bootstrap-static/'
uri = URI(url)
response = Net::HTTP.get(uri)
object = JSON.parse(response)
#testy = object["elements"]["first_name"]
end
I am able to access all the data inside of elements just fine, but when I add ["first_name"], I get the error: no implicit conversion of String into Integer
Seems a bit weird? Surely it should just pull in whatever is inside of "first_name", whether it's an integer, string etc?
Thanks
It's not the least bit strange.
object["elements"] is an array and when you call the [] method on a array the argument is coerced into an integer as its used to access array elements by index:
irb(main):052:0> []["foo"]
(irb):52:in `[]': no implicit conversion of String into Integer (TypeError)
Surely it should just pull in whatever is inside of "first_name", whether it's an integer, string etc?
In other languages like JavaScript you would just get null instead because what you expect to be a single object is actually an array.
Do yourself a big favor and move the HTTP call and JSON processing out of the controller and into a separate component that you can test in isolation. It will make it a lot easier to spot simple issues like this.
I'm new to json, there was a problem and I couldn't find a solution
I was given an api and when executing a get request, I get some object, but if there is no data in the object, an array is returned.
At the moment I was able to get Any?, instead of JSONArray or JSONObject, but there was a problem with converting Any? to the class
How to convert data to kotlin data class correctly?
returned object
returned array
The class I'm converting the json request to:
data class ProductInfo (var product:Product?,var specifications: JsonObject?,var supplements: Any?,var files:List<File>?,var feedback: Feedback?)
This seems something that the backend has to solve for you. They can give you a nullable array or just an empty array, whatever is more convenient, but implementing polymorphism is not something trivial.
Jackson makes polymorphism easier than Gson, however, it is always required some kind of anchor to know how to route the parsing, in this case, you don't have any.
Jackson uses an annotation and there you have indicate in which thing is going to pivot:
#JsonSubTypes.Type(value = Nothing.class, name = "????")
With Gson you have to implement your own JsonDeserializer but again, how do you know what type is it? If it can be cast to array then is nothing? Just writing that seems like an antipattern.
I am fairly new to Kotlin and I am currently dealing with JSON a lot.
I receive a JSON string from the server which I parse into JSONArray as below
var dataArray = JSONArray(String(resultVar!!))
But as far as I've seen JSONArray doesn't really give me enough capabilities to change it's data, it forces me to create a new JSONArray, if I'm not mistaken. So I thought I should use MutableList<JSONObject>, which seems good enough, but I can't find a way to parse JSONArray or the String into it.
How do I do this the easy way? Do I have to iterate through the JSONArray adding every single JSONObject one by one?
As a side question, should I just stick to JSONArray? Is there a way to manipulate the data inside it?
As far as it seems, there is no builtin method to convert JSONArray to List.
However, You can use Kotlin tricks to make the java code much shorter:
fun JSONArray.toMutableList(): MutableList<JSONObject> = MutableList(length(), this::getJSONObject)
Note that JSONArray may have values that are not JSONObject (but JSONArray for example). If you are not sure the array contains just object, use the following method:
fun JSONArray.toMutableList(): MutableList<Any> = MutableList(length(), this::get)
If you are using the JSON-java library and not the android one, then you can use JSONArray.toList() which returns a list you can modify.
I have created and added a CollaborativeString to the Model with name 'SomeString'. Now I want to access the same object from the Model using model.getRoot().get('SomeString') and convert it to CollaborativeString and call one function of CollaborativeString class.
How can I convert the CollaborativeObject returned by the model.getRoot().get('SomeString') to CollaborativeString?
I saw CollaborativeString inherits CollaborativeObject, but I dont know how exactly to convert base class object to derived one.
Thanks in advance.
The object returned by model.getRoot().get('SomeString') is already a CollaborativeString. You don't have to convert it. For example, if you want to append to the string, you could write
model.getRoot().get('SomeString').append('text to append');
I have many parameters to pass to the server using JAX-RS.
Is there a way to pass or AarryList with the URL?
You have a few options here.
Option 1: A query parameter with multiple values
You can supply multiple simple values for a single query parameter. For example, your query string might look like:
PUT /path/to/my/resource?param1=value1¶m1=value2¶m1=value3
Here the request parameter param1 has three values, and the container will give you access to all three values as an array (See Query string structure).
Option 2: Supply complex data in the PUT body
If you need to submit complex data in a PUT request, this is typically done by supplying that content in the request body. Of course, this payload can be xml (and bound via JAXB).
Remember the point of the URI is to identify a resource (RFC 3986, 3.4), and if this array of values is data that is needed to identify a resource then the URI is a good place for this. If on the other hand this array of data forms part of the new representation that is being submitted in this PUT request, then it belongs in the request body.
Having said that, unless you really do just need an array of simple values, I'd recommend choosing the Option 2. I can't think of a good reason to use URL-encoded XML in the URL, but I'd be interested to hear more about exactly what this data is.
We can get the Query parameters and corresponding values as a Map,
#GET
#Produces(MediaType.APPLICATION_JSON)
public void test(#Context UriInfo ui) {
MultivaluedMap<String, String> map = ui.getQueryParameters();
String name = map.getFirst("name");
String age = map.getFirst("age");
System.out.println(name);
System.out.println(age);
}