Related
I work on a laravel application that is a bit like a quiz. You create an exercise, you add questions and each question to answers.
Until then everything is fine.
My interest is to present statistics for each exercise after schoolchildren have answered the tests.
I want to be able to display the answer percentage for each question.
Here is an example of the data structure I have:
[
{
"id": 1,
"exercice_id": 1,
"question": "Lorem ipsum ?",
"responses": [
{
"id": 1,
"exercice_id": 1,
"question_id": 1,
"response_text": "Yes",
},
{
"id": 2,
"exercice_id": 1,
"question_id": 1,
"response_text": "No",
}
],
"choice": [
{
"id": 1,
"exercice_id": 1,
"question_id": 1,
"response_id": 1,
},
{
"id": 2,
"exercice_id": 1,
"question_id": 1,
"response_id": 1,
},
{
"id": 3,
"exercice_id": 1,
"question_id": 1,
"response_id": 2,
}
]
},
{
"id": 2,
"exercice_id": 1,
"question": "fake text ?",
"responses": [
{
"id": 3,
"exercice_id": 1,
"question_id": 2,
"response_text": "A",
},
{
"id": 4,
"exercice_id": 1,
"question_id": 2,
"response_text": "B",
},
{
"id": 5,
"exercice_id": 1,
"question_id": 2,
"response_text": "C",
}
],
"choice": [
{
"id": 4,
"exercice_id": 1,
"question_id": 2,
"response_id": 5,
},
{
"id": 5,
"exercice_id": 1,
"question_id": 2,
"response_id": 3,
}
]
}
]
I tried the groupBy method on several elements. but I have not found the formula yet.
return response()->json(Question::with('responses','choice')
->where('exercice_id',$exo->id)
//->groupBy('choice')
->get(),
200,[], JSON_NUMERIC_CHECK);
I have a response that looks like this
{
"id": 3,
"parent_id": 1,
"name": "QuickStart Intelligence",
"is_active": true,
"position": 2,
"level": 1,
"product_count": 590,
"children_data": [
{
"id": 53,
"parent_id": 3,
"name": "Find Training",
"is_active": true,
"position": 1,
"level": 2,
"product_count": 9,
"children_data": [
{
"id": 58,
"parent_id": 53,
"name": "Career Paths",
"is_active": true,
"position": 1,
"level": 3,
"product_count": 0,
"children_data": [
{
"id": 123,
"parent_id": 58,
"name": "Business Productivity",
"is_active": true,
"position": 1,
"level": 4,
"product_count": 245,
"children_data": []
},
{
"id": 70,
"parent_id": 58,
"name": "Creative & Design",
"is_active": true,
"position": 3,
"level": 4,
"product_count": 43,
"children_data": []
},
]
},
{
"id": 57,
"parent_id": 53,
"name": "Technology",
"is_active": true,
"position": 2,
"level": 3,
"product_count": 1,
"children_data": [
{
"id": 162,
"parent_id": 57,
"name": "Microsoft",
"is_active": true,
"position": 6,
"level": 4,
"product_count": 183,
"children_data": []
},
{
"id": 164,
"parent_id": 57,
"name": "Adobe",
"is_active": true,
"position": 8,
"level": 4,
"product_count": 34,
"children_data": []
}
]
}
{
"id": 100,
"parent_id": 53,
"name": "Certifications",
"is_active": true,
"position": 3,
"level": 3,
"product_count": 0,
"children_data": [
{
"id": 112,
"parent_id": 100,
"name": "Microsoft SQL Server 2012",
"is_active": true,
"position": 1,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 113,
"parent_id": 100,
"name": "Windows Server 2012 Microsoft Certifications",
"is_active": true,
"position": 2,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 114,
"parent_id": 100,
"name": "Visual Studio Microsoft Certifications",
"is_active": true,
"position": 3,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 115,
"parent_id": 100,
"name": "SharePoint 2013 Microsoft Certifications",
"is_active": true,
"position": 4,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 116,
"parent_id": 100,
"name": "Private Cloud Microsoft Certifications",
"is_active": true,
"position": 5,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 117,
"parent_id": 100,
"name": "Exchange Server 2013 Certifications",
"is_active": true,
"position": 6,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 118,
"parent_id": 100,
"name": "Lync Server 2013 Microsoft Certifications",
"is_active": true,
"position": 7,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 119,
"parent_id": 100,
"name": "Windows 8 Microsoft Certifications",
"is_active": true,
"position": 8,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 120,
"parent_id": 100,
"name": "Microsoft System Center Certifications",
"is_active": true,
"position": 9,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 121,
"parent_id": 100,
"name": "Microsoft Windows Server 2008 Certifications",
"is_active": true,
"position": 10,
"level": 4,
"product_count": 0,
"children_data": []
}
]
}
It goes on like that. I have initialized some variables in my class Categories like this
var categoryId = Int()
var parentId = Int()
var categoryName = String()
var isCategoryActive = Bool()
var categoryPostion = Int()
var categoryLevel = Int()
var categoryChildCount = Int()
var childArray = [Categories]()
I know that it should be done with a recursive call but somehow i am unable to wrap my head around the solution. I've tried looking on overflow but couldn't find anything relevant. this is how i am casting the json
class func getUserFromJson(_ jsonString: String) -> Categories {
let category = Categories()
do {
let dic = try JSONSerialization.jsonObject(with: jsonString.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!, options: .allowFragments) as! NSDictionary
if let id = dic.object(forKey: "id") as? Int {
category.categoryId = id
}
if let parentId = dic.object(forKey: "parent_id") as? Int {
category.parentId = parentId
}
if let name = dic.object(forKey: "name") as? String {
category.categoryName = name
}
if let isActive = dic.object(forKey: "is_active") as? Bool {
category.isCategoryActive = isActive
}
if let position = dic.object(forKey: "position") as? Int {
category.categoryPostion = position
}
if let level = dic.object(forKey: "level") as? Int {
category.categoryLevel = level
}
if let productCount = dic.object(forKey: "product_count") as? Int {
category.categoryChildCount = productCount
}
}
catch {
}
return category
}
How would i parse the children_data which is an array and has many children and again those children have children as you can see and some of them are empty so i need to check for that as well.
go like this.
let childrenData = dic["children_data"] as? NSArray
{
for data in childrenData
{
let child_dic = data as! NSDictionary
//Here Your Code.
}
}
You can use same for child of child.
I'm working on some code in which uses dynamic variables jsonResponse .
dynamic jsonResponse = JsonConvert.DeserializeObject(response);
This variable contains collection of hotel list in json format. From this collection I am getting roomlist collection in a new variable roomResponseList :
var roomResponseList = jsonResponse["hotels"]["hotels"][rooms].roomResponseList;
I am getting first room detail into **JObject responseRateKeys **:
foreach (var roomByResponse in roomResponseList)
{
JObject responseRateKeys = JObject.Parse(roomByResponse.ToString());
var boardNameListByResponse = responseRateKeys.AsJEnumerable().AsEnumerable()
.Select(t => t["rates"]["boardName"].ToString().Trim())
.Distinct()
.ToList();
}
But when I am trying to get any item list from JObject by using linq lambda, I am getting error,
"Cannot access child value on Newtonsoft.Json.Linq.JProperty."
Value of roomByResponse=
{ "code": "DBL.KG-NM", "name": "DOUBLE KING BED NON SMOKING", "rates": [ { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|RO|IWH25|1~1~0||N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NRF", "rateType": "RECHECK", "net": "186.04", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "RO", "boardName": "ROOM ONLY", "cancellationPolicies": [ { "amount": "149.63", "from": "2017-07-14T03:29:00+05:30" } ], "rooms": 1, "adults": 1, "children": 0, "dailyRates": [ { "offset": 1, "dailyNet": "93.02" }, { "offset": 2, "dailyNet": "93.02" } ] }, { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|BB|IWB25|1~1~0||N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NOR", "rateType": "RECHECK", "net": "238.92", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "BB", "boardName": "BED AND BREAKFAST", "rooms": 1, "adults": 1, "children": 0, "dailyRates": [ { "offset": 1, "dailyNet": "119.46" }, { "offset": 2, "dailyNet": "119.46" } ] }, { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|RO|IWH25|2~2~1|2|N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NRF", "rateType": "RECHECK", "net": "372.06", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "RO", "boardName": "ROOM ONLY", "cancellationPolicies": [ { "amount": "299.25", "from": "2017-07-14T03:29:00+05:30" } ], "rooms": 2, "adults": 2, "children": 1, "childrenAges": "2", "dailyRates": [ { "offset": 1, "dailyNet": "186.03" }, { "offset": 2, "dailyNet": "186.03" } ] }, { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|BB|IWB25|2~2~1|2|N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NOR", "rateType": "RECHECK", "net": "477.84", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "BB", "boardName": "BED AND BREAKFAST", "rooms": 2, "adults": 2, "children": 1, "childrenAges": "2", "dailyRates": [ { "offset": 1, "dailyNet": "238.92" }, { "offset": 2, "dailyNet": "238.92" } ] } ] }
Thank you
Pravesh Singh
change linq to
responseRateKeys["rates"].AsJEnumerable().Select(t=>t["boardName"]).Distinct().ToList()
This is all using ecmascript 6; Say I have a class
class Hero {
constructor(id,name){
this.id=id;
this.name=name;
}
}
And I want to have create an array of object of that type of class:
var HEROES = [
{ "id": 11, "name": "Mr. Nice" },
{ "id": 12, "name": "Narco" },
{ "id": 13, "name": "Bombasto" },
{ "id": 14, "name": "Celeritas" },
{ "id": 15, "name": "Magneta" },
{ "id": 16, "name": "RubberMan" },
{ "id": 17, "name": "Dynama" },
{ "id": 18, "name": "Dr IQ" },
{ "id": 19, "name": "Magma" },
{ "id": 20, "name": "Tornado" }
];
Is there a way to do this besides manually parsing the array and then doing New Hero() for each element? It would be nice to be able to do something like HEROES[] = hero.jsonExport() and then later var = hero.jsonImport(HEROES[i]). I've been looking around but without using Typescript this doesn't seem particularly easy. Am I missing something easy?
Thanks!
-Eric
Seems strange to want to import from a specific array instance - especially given that the order could change. It would probably be better to export a factory function which creates a new Hero based off the criteria:
class Hero {
constructor(id,name){
this.id=id;
this.name=name;
}
}
const HEROES = [
{ "id": 11, "name": "Mr. Nice" },
{ "id": 12, "name": "Narco" },
{ "id": 13, "name": "Bombasto" },
{ "id": 14, "name": "Celeritas" },
{ "id": 15, "name": "Magneta" },
{ "id": 16, "name": "RubberMan" },
{ "id": 17, "name": "Dynama" },
{ "id": 18, "name": "Dr IQ" },
{ "id": 19, "name": "Magma" },
{ "id": 20, "name": "Tornado" }
];
export default function createHero(index) {
const hero = HEROES[index];
return new Hero(hero.id,hero.name);
}
And the caller:
import createHero from "./hero";
const hero1 = createHero(1);
When I execute a query over an index like this:
{
"_source":["bar"] , "size":100,
"query": {
"match_all": {}
},
"filter": {
"type" : {
"value" : "foo"
}
}
}
the response includes index, type, etc. But I already know the index and type because I specified it. This information just bloats up the size of the json data. Is there a way to exclude these from the response?
This is what I get:
{
"took": 31,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 364024,
"max_score": 1,
"hits": [
{
"_index": "foo_bar",
"_type": "foo",
"_id": "asdjj123123",
"_score": 1,
"_source": {
"bar": "blablablabla"
}
}
,...
What I want is something like this, so a response without type,score,index:
{
"took": 31,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 364024,
"max_score": 1,
"hits": [
{
"_id": "asdjj123123",
"_source": {
"bar": "blablablabla"
}
}
,...
Yes, as of ES 1.6, you can use response filtering and using the filter_path parameter in the query enumerate only what you need in the response:
curl -XGET 'localhost:9200/foo_bar/foo/_search?pretty&filter_path=hits.total,hits.max_score,hits.hits._id,hits.hits._source'