Json Parsing in Angular2 - arrays
I have a array of jsons that I want to parse to my class Tournament.
The class Tournament looks like this:
export class Tournament { constructor (public id: number,
public name: string,
public date: string,
public pointsWin: string,
public pointsDraw: string,
public groupSize: string,
public groupPhase: string,
public system: string,
public teams: Team[]
){ }
}
The class Team looks like this:
export class Team { constructor (public id: number,
public name: string,
public rank: number,
public occupied: boolean,
public group: string
){ }
}
And the JSON that i receive looks like this:
[{"id":1,
"name":"Schulcup1",
"date":
{"year":2017,"month":"MARCH","era":"CE","dayOfYear":83,"dayOfWeek":"FRIDAY","leapYear":false,"dayOfMonth":24,"monthValue":3,"chronology":{"calendarType":"iso8601","id":"ISO"}},
"pointsWin":0,
"pointsDraw":0,
"groupSize":0,
"groupPhase":null,
"system":null,
"teams":[
{"id":1,"name":"Team1","rank":8,"occupied":false,"group":"A"},
{"id":2,"name":"Team2","rank":1,"occupied":false,"group":"A"},
{"id":3,"name":"Team3","rank":4,"occupied":false,"group":"A"},
{"id":4,"name":"Team4","rank":16,"occupied":false,"group":"A"},
{"id":5,"name":"Team5","rank":2,"occupied":false,"group":"B"},
{"id":6,"name":"Team6","rank":16,"occupied":false,"group":"B"},
{"id":7,"name":"Team7","rank":8,"occupied":false,"group":"B"},
{"id":8,"name":"Team8","rank":4,"occupied":false,"group":"B"}],
"active":true},
{"id":2,
"name":"Schulcup2",
"date":
{"year":2017,"month":"MARCH","era":"CE","dayOfYear":82,"dayOfWeek":"THURSDAY","leapYear":false,"dayOfMonth":23,"monthValue":3,"chronology":{"calendarType":"iso8601","id":"ISO"}},
"pointsWin":0,
"pointsDraw":0,
"groupSize":0,
"groupPhase":null,
"system":null,
"teams":[
{"id":48,"name":"Team1","rank":16,"occupied":false,"group":"A"},
{"id":49,"name":"Team2","rank":2,"occupied":false,"group":"A"},
{"id":50,"name":"Team3","rank":4,"occupied":false,"group":"A"},
{"id":51,"name":"Team4","rank":1,"occupied":false,"group":"A"},
{"id":52,"name":"Team5","rank":16,"occupied":false,"group":"B"},
{"id":53,"name":"Team6","rank":8,"occupied":false,"group":"B"},
{"id":54,"name":"Team7","rank":16,"occupied":false,"group":"B"},
{"id":55,"name":"Team8","rank":16,"occupied":false,"group":"B"}],
"active":true}]
If I try to parse the JSON with JSON.parse() I get an error that there is an unexpected token in JSON at position 0 most likely because of the [ since it is an array of JSON but how can I iterate through it?
And also how can I iterate through the teams in the JSON to create an array of teams?
It is already parsed. Just use it as an object!
No need to use classes here, simply cast your JSON to interfaces, if you really do not need classes. Casting to interfaces is easy and painless ;) And no need of using JSON.parse here.
So use interfaces if you can, here is Tournament:
export interface Tournament {
public id: number,
public name: string,
public date: string,
public pointsWin: string,
public pointsDraw: string,
public groupSize: string,
public groupPhase: string,
public system: string,
public teams: Team[]
}
Service:
getTournaments(): Observable<Tournament[]> {
return this.http.get('src/data.json')
.map(res => res.json() as Tournament[])
}
TS:
tournaments: Tournament[] = [];
getTournaments() {
this.service.getTournaments()
.subscribe(data => {
this.tournaments = data;
})
}
DEMO
Related
can't parse json to object in flutter
Seeking your help. Actually, I've created an object model class. However, when I tried to parse the JSON response to the object model but error as below : JSON Response "value": [ { "CardCode": "C20000", "CardName": "Maxi Teq", "DocCur": "AUD", "DocEntry": 793, "DocNum": 793, "DocTotal": 99.0, "DocType": "I", "U_Driver": "addon", "U_GLINK": "https://goo.gl/maps/tQJh7Zj9fpUzQqcv9" }, { "CardCode": "C20000", "CardName": "Maxi Teq", "DocCur": "AUD", "DocEntry": 795, "DocNum": 795, "DocTotal": 99.0, "DocType": "I", "U_Driver": "addon", "U_GLINK": "https://goo.gl/maps/tQJh7Zj9fpUzQqcv9" } ] Model All the variables are string, but the JSON response includes int. import 'package:json_annotation/json_annotation.dart'; part 'order.g.dart'; #JsonSerializable(explicitToJson: true) class order { String? cardCode; String? cardName; String? docCur; String? docEntry; String? docNum; String? docTotal; String? docType; String? uDriver; String? uGLINK; order({this.cardCode, this.cardName, this.docCur, this.docEntry, this.docNum, this.docTotal, this.docType, this.uDriver, this.uGLINK}); factory order.fromJson(Map<String,dynamic> data) => _$orderFromJson(data); Map<String,dynamic> toJson() => _$orderToJson(this); } Main Class Map<String, dynamic> orderMap = json.decode(orderJson); List<dynamic> orderList = orderMap["value"]; print("Method-------------$orderList"); List<order> list = orderList.map((val) => order.fromJson(val)).toList(); print(list.toString()); Error Response I/flutter (26244): [Instance of 'order', Instance of 'order', Instance of 'order', Instance of 'order', Instance of 'order'] Thank you
That is not an error. That is the result of: print(list.toString()); What you are lacking is a toString() method on the class order, so it just prints that you have a list of instances of type order. Add something like this to your order class: #override String toString() { return 'order(cardCode: $cardCode, cardName: $cardName, docCur: $docCur, docEntry: $docEntry, docNum: $docNum, docTotal: $docTotal, docType: $docType, uDriver: $uDriver, uGLINK: $uGLINK)'; }
Use orderList.elementAt(0) to get a map of value.
Can not acces array from model in Angular
I've been trying to acces an array from my object. This is my class: export class Competence { private _id: string; private _name: string; private _isFinished: boolean; private _subCompetences: string[]; constructor(name: string, isFinished: boolean, subCompetences: string[]) { this._name = name; this._isFinished = isFinished; this._subCompetences = subCompetences; } With the getters and setters aswell. I've been trying to call the subCompetences from a Competence object in this code: export class StudentModuleDetailsComponent implements OnInit { private competences: Competence[] = []; private subcompetences: SubCompetence[] = []; constructor() { } ngOnInit() { this.getData() } private showSubCompetences(competence: Competence) { this.chosenSubCompetences = []; console.log(competence.subCompetences) the showSubCompetences() method gets called with a click event and the clicked competence is given as a parameter. The competence object is initialized in this method that works perfectly fine. private getData() { this._apiModulesDataService.getModuleById(this._studentDataService.moduleId).subscribe( data => { this._apiModulesDataService; var self = this; this.module = data[0]; this.module.competences.forEach(function (comp) { self._apiCompetenceDataService.getCompetenceById(comp).subscribe( c => { if (!self.competences.includes(c)) { self.competences.push(c[0]); } } ); }); }); } } Now when I click the competence it only prints out undefined. And when I only print the competence like so console.log(competence) I get this Json as output {id: "f39356b0-e2a9-11e8-858b-23856324831a", isfinished: null, name: "Knippen", subcompetences: Array(2)} id: "f39356b0-e2a9-11e8-858b-23856324831a" isfinished: null name: "Knippen" subcompetences: Array(2) 0: "08638e20-e2aa-11e8-858b-23856324831a" 1: "0d772570-e2aa-11e8-858b-23856324831a" length: 2 How do i fix this?
Hmm, first I suggest fixing your model in order to avoid any future errors: export class Competence { private _id: string; private _name: string; private _isfinished: boolean; private _subcompetences: string[]; constructor(name: string, isFinished: boolean, subCompetences: string[]) { this._name = name; this._isfinished = isFinished; this._subcompetences = subCompetences; } ... } Then, try logging subcompetences like this: console.log(competence.subcompetences) Also, with new model you should be able to properly get isfinished property as well... Hope this helps.
Convert array of custom object to String in swift4
I am new to Swift and have an issue converting an array of custom object, to String. This is my response class Tickets public struct Tickets: Codable { public let name: String! public let status: String! public let department: String! } After the webservice call i get following response and it would be mapped to Tickets class. Now, I have an array of "Tickets" as [Tickets] described below. "tickets": [ { "name": "d5b5d618-8a74-4e5f", "status": "VALID", "department": "IT" }, { "name": "a58f54b5-9420-49b6", "status": "INVALID", "department": "Travel" } ] Now, can I convert an array of [Tickets] to String? If so, how? Also, how to get it back as [Tickets] from a class of String. I want to store it into UserDefaults after converting it to String, and retrieve it later
First of all: Never declare properties or members in a struct or class as implicit unwrapped optional if they are supposed to be initialized in an init method. If they could be nil declare them as regular optional (?) otherwise as non-optional (Yes, the compiler won't complain if there is no question or exclamation mark). Just decode and encode the JSON with JSONDecoder() and JSONEncoder() let jsonTickets = """ {"tickets":[{"name":"d5b5d618-8a74-4e5f","status":"VALID","department":"IT"},{"name":"a58f54b5-9420-49b6","status":"INVALID","department":"Travel"}]} """ public struct Ticket: Codable { public let name: String public let status: String public let department: String } do { let data = Data(jsonTickets.utf8) let tickets = try JSONDecoder().decode([String:[Ticket]].self, from: data) print(tickets) let jsonTicketsEncodeBack = try JSONEncoder().encode(tickets) jsonTickets == String(data: jsonTicketsEncodeBack, encoding: .utf8) // true } catch { print(error) }
$http post from angularjs to C# MVC view model, have correct List count but the values are null
I have the following post request using $http var registration = { userId: 23, groupings: [ { Id: 1, Name: 'Test Group 1', Description: 'Yo' }, { Id: 4, Name: 'Test Group 4', Description: 'Er' } ] } $http({ method: 'POST', url: url, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, data: $.param({ code: eventCode, UserId: registration.userId Groupings: registration.groupings }) }) And then on my action [HttpPost] public ActionResult New(string code, RegistrationVM model) { : : : } public class RegistrationVM { public int UserId {get;set;} public IEnumerable<GroupingVM> Groupings { get; set; } } public class GroupingVM{ public int Id {get;set;} public string Name {get;set;} public string Description {get;set;} public int AnotherPropertyId {get;set;} public ANewClass ANewClass {get;set;} } Whenever the post happens, I would have the model properties reflect what I posted except the IEnumerable (Groupings) property. Let's say I post with 2 objects on the Groupings property, when I go to the Action, the Groupings property will have a count of 2 but every instance would have either NULL or 0 values on each object. I'm trying to figure out how I'm messing the post.
First, make the controller take one object as a parameter. Add the "string code" to the RegistrationVM and remove the string code as a parameter. Then do the same when you create the client object. Secondly, instead of using the content type "x-www-form-urlencoded" use the "application/json" which is better suited to pass objects. Before posting the data you need to stringify the obj with data: JSON.stringify(registration) For more details, read the comments on the question.
UpdateOperations embedded array
I have an structure like this: { _id: 123, bs: [ { _id: 234, cs: [ { _id: 456, ds : [ { _id: 678, emails[ "email#gmail.com" ] } ] } ] } ] } My classes in Morphia seems like this #Entity public class A { #Id private ObjectId id; #Embedded private List<B> bs; } public class B { private ObjectId id; #Embedded private List<C> cs; } public class C { private ObjectId id; #Embedded private List<D> ds; } public class D { private ObjectId id; #Embedded private List<String> emails; } What I am trying to do is insert an email inside embedded array with Morphia without retrieve all element A and use updateFirst. This is the query I am trying execute Query<Event> query = this.basicDAO.createQuery(); query.criteria(Mapper.ID_KEY).equal(new ObjectId(aID)); query.criteria("bs.cs.id").equal(new ObjectId(cID)); query.criteria("bs.cs.ds.id").equal(dID); UpdateOperations<Event> updateOps = this.basicDAO.createUpdateOperations().set("bs.cs.ds.$.emails", email); this.basicDAO.update(query, updateOps); I also read about this post Update an item in an array that is in an array with said that $ operator does not work "with queries that traverse nested arrays". So I tried something like: D d = new D(dID); C c = new C(new ObjectId(cID)); Query<Event> query = this.basicDAO.createQuery(); query.criteria(Mapper.ID_KEY).equal(new ObjectId(aID)); query.field("bs.cs").hasThisElement(c); query.field("bs.cs.ds").hasThisElement(d); UpdateOperations<Event> updateOps = this.basicDAO.createUpdateOperations().set("bs.cs.ds.emails", email); this.basicDAO.update(query, updateOps); However it still doesn't work. Any idea how solve this? The error message that I receive is cannot use the part ... to transverse the element
Based on your stand-in use case, I think you should "invert" your schema. Each document will represent a lecture and will be tagged with its theme, edition, and event: { "_id" : ObjectId("54da1ff0a9ce603a239c3075"), "event" : "X0004", "edition" : "A0002, "theme" : "RT0005", "votes" : 22 } event, edition, and theme can be some kind of identifiers, and might be references to event, edition, and theme documents in other collections. To cast a vote for a particular lecture, just update it by _id: db.test.update({ "_id" : ObjectId("54da1ff0a9ce603a239c3075") }, { "$inc" : { "votes" : 1 } }) While I don't know your full requirements, I think this is a better basic design given your example use case.