I'm looking for some direction on mapping json to an object. I'm trying to use live data from a web api. When I run the code I get the following error: Class '_LinkedHashMap' has no instance getter 'iterator'.
Here's what I'm trying to do:
import 'package:angular/angular.dart';
import 'dart:convert';
#NgController(
selector: '[movies]',
publishAs: 'ctrl'
)
class MoviesController {
// vars
bool moviesLoaded = false;
String nameToSearch = 'life';
Http _http;
List<MovieBrief> movies = [];
// constructor
MoviesController( Http this._http ) {
_loadData();
}
// functions
void _loadData() {
moviesLoaded = false;
// _http.get('http://www.omdbapi.com/?s=' + nameToSearch)
_http.get('movies_briefs.json')
.then((HttpResponse response) {
for (Map mb in response.data) {
movies.add(new MovieBrief.fromJsonMap(mb));
}
moviesLoaded = true;
},
onError: (Object obj) {
print(obj);
}
);
}
}
class MovieBrief {
String title;
String year;
String imdbId;
String type;
MovieBrief(this.title, this.year, this.imdbId, this.type);
String toJsonString() {
Map data = {"Search": [
{
"Title" : title,
"Year" : year,
"imdbID" : imdbId,
"Type" : type
}]};
return JSON.encode(data);
}
factory MovieBrief.fromJsonMap( Map json ) {
return new MovieBrief( json['Title'], json['Year'], json['imdbID'], json['Type']);
}
}
Here's a sample of the json results returned by the web api:
{
"Search": [
{
"Title": "Life of Pi",
"Year": "2012",
"imdbID": "tt0454876",
"Type": "movie"
},
{
"Title": "Life Is Beautiful",
"Year": "1997",
"imdbID": "tt0118799",
"Type": "movie"
},
{
"Title": "Life of Brian",
"Year": "1979",
"imdbID": "tt0079470",
"Type": "movie"
},
{
"Title": "It's a Wonderful Life",
"Year": "1946",
"imdbID": "tt0038650",
"Type": "movie"
},
{
"Title": "A Bug's Life",
"Year": "1998",
"imdbID": "tt0120623",
"Type": "movie"
},
{
"Title": "A Bug's Life",
"Year": "1998",
"imdbID": "tt0174716",
"Type": "movie"
},
{
"Title": "The Tree of Life",
"Year": "2011",
"imdbID": "tt0478304",
"Type": "movie"
},
{
"Title": "The Life Aquatic with Steve Zissou",
"Year": "2004",
"imdbID": "tt0362270",
"Type": "movie"
},
{
"Title": "Lara Croft Tomb Raider: The Cradle of Life",
"Year": "2003",
"imdbID": "tt0325703",
"Type": "movie"
},
{
"Title": "Dan in Real Life",
"Year": "2007",
"imdbID": "tt0480242",
"Type": "movie"
}
]
}
Have you looked at the json_object package? This parses a json string and creates properties on the resulting object, in your case you would have say film.title, film.year etc. You can store each json_object in a list.
I'm guessing response.data is a map, so you can't iterate on it with a for loop. You need to either use response.data.keys, response.data.values, or use the forEach method on the map, which provides both.
response.data.forEach((key, value) => movies.add(Etc)
As per the other response, something like json_object is also worth looking into, but that's a way of avoiding hard coding the field mappings.
Related
I have a simple model class from which I need to generate the schema on Mongo DB Atlas. But I'm having troubles when it comes to defining RealmList<String> inside a JSON schema. If I insert "array" as a bsonType, I get an error. What should I write instead?
Model class:
class Note : RealmObject {
#PrimaryKey
var _id: ObjectId = ObjectId.create()
var title: String = ""
var description: String = ""
var images: RealmList<String> = realmListOf()
var date: RealmInstant = RealmInstant.from(System.currentTimeMillis(),0)
}
Current Schema:
{
"bsonType": "object",
"properties": {
"_id": {
"bsonType": "objectId"
},
"title": {
"bsonType": "string"
},
"description": {
"bsonType": "string"
},
"images": {
"bsonType": "array"
},
"date": {
"bsonType": "date"
}
},
"required": [
"_id",
"title",
"description",
"images",
"date"
],
"title": "Note"
}
I am not sure which mode you're using but if you're in development mode, when you add an object in the SDK, the server will automatically generate a matching object, as long as the changes are additive, like adding a new object or property
In the queston, the 'images' bson definition looks incomplete
"images": {
"bsonType": "array"
},
While it is an array, it's an array of strings so I believe it should look more like this
"images": {
"bsonType": "array",
"items": {
"bsonType": "string"
}
}
Where the type of items is defined as a string
The have an issue where I am unable to call nested JSON Objects from a scraped website. The scraping process works prefectly, but the JSON Serializing part is the only issue. My code is shown below:
private void GetHtmlAsync()
{
var url = "https://opentdb.com/api.php?amount=10";
var httpClient = new HttpClient();
var html = httpClient.GetStringAsync(url);
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(MyDetail));
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(html.Result));
stream.Position = 0;
MyDetail dataContractDetail = (MyDetail) jsonSerializer.ReadObject(stream);
text.text = "" + dataContractDetail.results[1];
//text.text = string.Concat("Test: ", dataContractDetail.question, " " + dataContractDetail.correct_answer);
}
public class MyDetail
{
[DataMember]
public Dictionary<string, questions> results
{
get;
set;
}
public class questions
{
public string question { get; set; }
public string correct_answer { get; set; }
}
[DataMember]
public string response_code
{
get;
set;
}
}
This code is the code that does not work, in that I try to call the first object in results by doing "results[1]", which returns an error after I attach, say, "question" to it by doing "results[1].question". This syntax seems reasonable, so I don;t understand why it is not working. My JSON File is shown below:
{
"response_code": 0,
"results": [
{
"category": "Entertainment: Video Games",
"type": "multiple",
"difficulty": "medium",
"question": "What is the name of the virus in "Metal Gear Solid 1"?",
"correct_answer": "FOXDIE",
"incorrect_answers": [
"FOXENGINE",
"FOXALIVE",
"FOXKILL"
]
},
{
"category": "Geography",
"type": "multiple",
"difficulty": "easy",
"question": "What is the official language of Costa Rica?",
"correct_answer": "Spanish",
"incorrect_answers": [
"English",
"Portuguese",
"Creole"
]
},
{
"category": "Entertainment: Video Games",
"type": "multiple",
"difficulty": "medium",
"question": "In Fallout 4, which type of power armor is first encountered in the early mission "When Freedom Calls" in a crashed Vertibird?",
"correct_answer": "T-45",
"incorrect_answers": [
"T-51",
"T-60",
"X-01"
]
},
{
"category": "Politics",
"type": "boolean",
"difficulty": "medium",
"question": "George W. Bush lost the popular vote in the 2004 United States presidential election.",
"correct_answer": "False",
"incorrect_answers": [
"True"
]
},
{
"category": "Entertainment: Video Games",
"type": "multiple",
"difficulty": "medium",
"question": "In "Halo 2", what is the name of the monitor of Installation 05?",
"correct_answer": "2401 Penitent Tangent",
"incorrect_answers": [
"343 Guilty Spark",
"031 Exuberant Witness",
"252 Biodis Expolsion"
]
},
{
"category": "Entertainment: Books",
"type": "multiple",
"difficulty": "medium",
"question": "The book "Fahrenheit 451" was written by whom?",
"correct_answer": "Ray Bradbury",
"incorrect_answers": [
"R. L. Stine",
"Wolfgang Amadeus Mozart",
"Stephen King"
]
},
{
"category": "Entertainment: Cartoon & Animations",
"type": "multiple",
"difficulty": "hard",
"question": "In "Rick and Morty", from which dimension do Rick and Morty originate from?",
"correct_answer": "C-137",
"incorrect_answers": [
"J1977",
"C-136",
"C500-a"
]
},
{
"category": "Entertainment: Video Games",
"type": "multiple",
"difficulty": "hard",
"question": "In which game did the character "Mario" make his first appearance?",
"correct_answer": "Donkey Kong",
"incorrect_answers": [
"Super Mario Bros.",
"Super Mario Land",
"Mario Bros."
]
},
{
"category": "Entertainment: Film",
"type": "multiple",
"difficulty": "hard",
"question": "What was Humphrey Bogart's middle name?",
"correct_answer": "DeForest",
"incorrect_answers": [
"DeWinter",
"Steven",
"Bryce"
]
},
{
"category": "Entertainment: Cartoon & Animations",
"type": "boolean",
"difficulty": "medium",
"question": "In "Avatar: The Last Airbender" and "The Legend of Korra", Lavabending is a specialized bending technique of Firebending.",
"correct_answer": "False",
"incorrect_answers": [
"True"
]
}
]
}
There are many issues in your code. I don't know all the libraries you are using but here is how I would do it.
First of all you start a GetStringAsync but you continue immediately without waiting for results. I don't know all the libraries you are using ofcourse maybe it is supposed to be like that?
However I would rather use Unity's UnityWebRequest.Get
private void GetHtmlAsync()
{
StartCoroutine(DownloadJson());
}
private IEnumerator DownloadJson()
{
var url = "https://opentdb.com/api.php?amount=10";
using(var uwr = UnityWebRequest.Get(url))
{
// send the request and wait for result
yield return uwr.SendWebRequest();
// Check for success!
if(uwr.isNetworkError || uwr.isHttpError || !string.IsNullOrWhiteSpace(uwr.error))
{
Debug.LogError($"Download failed with {uwr.responseCode} reason: {uwr.error}", this);
yield break;
}
var json = uwr.DownloadHandler.text;
// ... se below
}
}
Again I don't know your JSON library but your class seems not to match the JSON data structure which would be (by simply jamming it through json2csharp)
[Serializable]
public class Result
{
public string category;
public string type;
public string difficulty;
public string question;
public string correct_answer;
public List<string> incorrect_answers;
}
[Serializable]
public class MyDetail
{
public int response_code;
public List<Result> results;
}
for Unity I would use [Serializable] and also remove all the {get;set} in order to not use properties but fields.
Then you could simply use Unity's JsonUtility
...
MyDetail dataContractDetail = JsonUtility.FromJson<MyDetail>(json);
Then as mentioned in the comments note that array indices in c# are 0-based so the first element would be
var firstResult = dataContractDetail.results[0];
Now the question is what do you want to see on your text? The firstResult is no string but rather a class having various members! You could e.g. want to display the question like
text.text = firstResult.question;
I am working on an API for my Portal, to provide users the ability to update there data via API directly from there internal Systems.
Liferay 6.2 bundle Tomcat. (for api call testing I use soapUI)
The get Methods work fine, (I have getAll, and getByID). getByID returns a JSON Object like this:
{
"ID": "ORGANIZATIONID",
"type": "ORGANIZATIONTYPE",
"name": "ORGANIZATIONNAME",
"URI": "ORGANIZATIONNAMEURI"
"date of inclusion": "INCLUTIONDATE",
"last activities": "LASTMODIFIEDDATE",
"address": {
"name of host institution": "NAMEOFHOSTINSTITUTE",
"street1": "STREET1",
"street2" : "STREET2",
"zip": "ZIP",
"city": "CITY",
"country": "COUNTRY",
},
"url": [{"ORGANIZATIONURL"}],
"main contact": {
"first name": "FIRSTNAME",
"last name" : "LASTNAME",
"phone": "PHONE",
"email": "MAINCONTACTEMAIL"
},
"type of host institution" : "TYPEOFHOSTINSTITUTE",
"diseases": [
{
"name": "DISEASENAME1",
"number": "SAMPLECOUNT",
"gene": "GENE",
"orphacode": "ORPHA"
"icd10": "ICD",
"omim": "OMIM";
"synonym": "SYNONYM"
},
{
"name": "DISEASENAME2",
"number": "SAMPLECOUNT",
"gene": "GENE",
"orphacode": "ORPHA"
"icd10": "ICD",
"omim": "OMIM";
"synonym": "SYNONYM"
}
]
}
I would like to have an API for Updating the diseases information for an organization. I have created a URL service where everything is in the url as parameters, but I would like to have it that the in the url only the id parameter is send and the rest of the information in a JSON object. Like:
/api/jsonws/....-portlet...../updateregbb/organization-id/16016/
and the data:
{
"ID": "ORGANIZATIONID",
"diseases": [
{
"name": "DISEASENAME1",
"number": "SAMPLECOUNT",
"gene": "GENE",
"orphacode": "ORPHA"
"icd10": "ICD",
"omim": "OMIM";
"synonym": "SYNONYM"
},
{
"name": "DISEASENAME2",
"number": "SAMPLECOUNT",
"gene": "GENE",
"orphacode": "ORPHA"
"icd10": "ICD",
"omim": "OMIM";
"synonym": "SYNONYM"
}
]
}
But I could not find a solution how I can read the JSON data:
#JSONWebService(value = "updateregbb", method = "POST")
public void updateregbb2(long organizationId, Map jsondata) {
#JSONWebService(value = "updateregbb", method = "POST")
public void updateregbb(long organizationId, JSONObject json) {
How can I get the Data into my function?
Thanks for the help,
Best
Robert
I'm trying to use JsonBuilder with Groovy to dynamically generate JSON. I want to create a JSON block like:
{
"type": {
"__type": "urn",
"value": "myCustomValue1"
},
"urn": {
"__type": "urn",
"value": "myCustomValue2"
},
"date": {
"epoch": 1265662800000,
"str": "2010-02-08T21:00:00Z"
},
"metadata": [{
"ratings": [{
"rating": "NR",
"scheme": "eirin",
"_type": {
"__type": "urn",
"value": "myCustomValue3"
}
}],
"creators": [Jim, Bob, Joe]
}]
}
I've written:
def addUrn(parent, type, urnVal) {
parent."$type" {
__type "urn"
"value" urnVal
}
}
String getEpisode(String myCustomVal1, String myCustomVal2, String myCustomVal3) {
def builder = new groovy.json.JsonBuilder()
def root = builder {
addUrn(builder, "type", myCustomVal1)
addUrn(builder, "urn", "some:urn:$myCustomVal2")
"date" {
epoch 1265662800000
str "2010-02-08T21:00:00Z"
}
"metadata" ({
ratings ({
rating "G"
scheme "eirin"
addUrn(builder, "_type", "$myCustomVal3")
})
creators "Jim", "Bob", "Joe"
})
}
return root.toString();
}
But I've run into the following issues:
Whenever I call addUrn, nothing is returned in the string. Am I misunderstanding how to use methods in Groovy?
None of the values are encapsulated in double (or single) quotes in the returned string.
Anytime I use a {, I get a '_getEpisode_closure2_closure2#(insert hex)' in the returned value.
Is there something wrong with my syntax? Or can someone point me to some example/tutorial that uses methods and/or examples beyond simple values (e.g. nested values within arrays).
NOTE: This is a watered down example, but I tried to maintain the complexity around the areas that were giving me issues.
You have to use delegate in addUrn method instead of
passing the builder on which you are working.
It is because you are doing a toSting() or toPrettyString() on root instead of builder.
Solved if #2 is followed.
Sample:
def builder = new groovy.json.JsonBuilder()
def root = builder {
name "Devin"
data {
type "Test"
note "Dummy"
}
addUrn(delegate, "gender", "male")
addUrn(delegate, "zip", "43230")
}
def addUrn(parent, type, urnVal) {
parent."$type" {
__type "urn"
"value" urnVal
}
}
println builder.toPrettyString()
Output:-
{
"name": "Devin",
"data": {
"type": "Test",
"note": "Dummy"
},
"gender": {
"__type": "urn",
"value": "male"
},
"zip": {
"__type": "urn",
"value": "43230"
}
}
I'd like to be able to search the following JSON object for objects containing the key 'location' then get in return an array or json object with the 'name' of the person plus the value of location for that person.
Sample return:
var matchesFound = [{Tom Brady, New York}, {Donald Steven,Los Angeles}];
var fbData0 = {
"data": [
{
"id": "X999_Y999",
"location": "New York",
"from": {
"name": "Tom Brady", "id": "X12"
},
"message": "Looking forward to 2010!",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/X999/posts/Y999"
},
{
"name": "Like",
"link": "http://www.facebook.com/X999/posts/Y999"
}
],
"type": "status",
"created_time": "2010-08-02T21:27:44+0000",
"updated_time": "2010-08-02T21:27:44+0000"
},
{
"id": "X998_Y998",
"location": "Los Angeles",
"from": {
"name": "Donald Steven", "id": "X18"
},
"message": "Where's my contract?",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/X998/posts/Y998"
},
{
"name": "Like",
"link": "http://www.facebook.com/X998/posts/Y998"
}
],
"type": "status",
"created_time": "2010-08-02T21:27:44+0000",
"updated_time": "2010-08-02T21:27:44+0000"
}
]
};
#vsiege - you can use this javascript lib (http://www.defiantjs.com/) to search your JSON structure.
var fbData0 = {
...
},
res = JSON.search( fbData0, '//*[./location and ./from/name]' ),
str = '';
for (var i=0; i<res.length; i++) {
str += res[i].location +': '+ res[i].from.name +'<br/>';
}
document.getElementById('output').innerHTML = str;
Here is a working fiddle;
http://jsfiddle.net/hbi99/XhRLP/
DefiantJS extends the global object JSON with the method "search" and makes it possible to query JSON with XPath expressions (XPath is standardised query language). The method returns an array with the matches (empty array if none were found).
You can test XPath expressions by pasting your JSON here:
http://www.defiantjs.com/#xpath_evaluator