I'm using jqgrid for tree structure in a table.I'm providing drag and drop row functionality.I want to call a callback method on drag event.Because I want to adjust the hierarchy of the rows which is a column having number in it ex:1.1,1.2.1,...Can I do this.
The following is the code I wrote inside my html:
jQuery(function(){
jQuery("#tree").tableDnD({scrollAmount:0});
jQuery("#tree").jqGrid({
url:'jsonSamplePots.json',
datatype: "json",
colNames: ["Task Id", "Task No.", "Task Name", "Priority", "Start Date", "End Date", "Task Type", "Link", "Link RelationShip", "Resources(Units)", "Reference"],
colModel: [
{name:'id', index:'id', width: 30, hidden: true, key: true},
{name:'no', width:80, sortable:false,editable:true},
{name:'name', width:150, sortable:false,editable:true},
{name:'priority', width:80, sortable:false,editable:true},
{name:'sDate', width:80, sortable:false},
{name:'eDate', width:80, sortable:false},
{name:'type', width:120, sortable:false},
{name:'link', width:80, sortable:false},
{name:'rship', width:80, sortable:false},
{name:'resources', width:300, sortable:false},
{name:'ref', width:80, sortable:false},
],
rowNum:10,
rowList:[10,20,30],
pager: '#pcelltbl',
sortname: 'id',
viewrecords: true,
sortorder: "desc",
caption:"Tasks",
cellEdit:true,
treeGrid: true,
ExpandColumn:'name',
gridComplete: function() {
//alert("gridcomplete");
$("#_empty","#tree").addClass("nodrag nodrop");
jQuery("#tree").tableDnDUpdate();
},
I'm using the following json data:
{
"total": "1",
"page": "1",
"records": "2",
"rows": [
{"id": "1", "cell": ["1", "Super <b>Item</b>", "300", "0", "", "false", "true", "true"]},
{"id": "2", "cell": ["2", "<a href='http://www.google.com'>Google</a>", "100", "1", "1", "false", "false", "true"]},
{"id": "3", "cell": ["3", "Sub Item 1", "50", "2", "2", "true", "true", "true"]},
{"id": "4", "cell": ["4", "Sub Item 2", "25", "2", "2", "false", "false", "true"]},
{"id": "5", "cell": ["5", "Sub-sub Item 1", "25", "3", "4", "true", "true", "true"]},
{"id": "6", "cell": ["6", "Sub Item 3", "25", "2", "2", "true", "true", "true"]},
{"id": "7", "cell": ["7", "<span style='background-color:LightGreen; color:Tomato'>Item 2</span>", "200", "1", "1", "false", "true", "true"]},
{"id": "8", "cell": ["8", "Sub Item 1", "100", "2", "7", "false", "false", "true"]},
{"id": "9", "cell": ["9", "Sub-sub Item 1", "50", "3", "8", "true", "true", "true"]},
{"id": "10", "cell": ["10", "Sub-sub Item 2", "50", "3", "8", "true", "true", "true"]},
{"id": "11", "cell": ["11", "Sub Item 2", "100", "2", "7", "true", "true", "true"]}
]
}
Thanks in advance
Related
I want to get unique data from a duplicates data at Hyperledger Composer. How can I achieve this?
suppose I have a data like this
[
{
"$class": "org.stock.mynetwork.Commodity",
"time": "1",
"dataType" : "in",
"productName": "A",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "2",
"dataType": "in",
"productName": “A",
"quantity": 1,
"country": "Unknown",
"owner": "BABA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "3",
"dataType": "out",
"productName": "C",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "4",
"dataType": "in",
"productName": "C",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "5",
"dataType": "out",
"productName": "B",
"quantity": 1,
"country": "Unknown",
"owner": "BABA"
}
]
what I want to get is a list of unique Product name.
So the result I want maybe something like this
[
{
"$class": "org.stock.mynetwork.Commodity",
"time": "1",
"dataType" : "in",
"productName": "A",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "3",
"dataType": "out",
"productName": "C",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "5",
"dataType": "out",
"productName": "B",
"quantity": 1,
"country": "Unknown",
"owner": "BABA"
}
]
I don't really care about the sequence. I just want to know what productName do I have in my database.
I think this needs something to do with the logic.js part. I can do queries and get the data using post method. But I don't know how to do that using logic.js
I don't know how to get the data from logic.js. The example at official page only delete or trade. They don't return a new data.
any help is appreciated. I want to know how to send data using the js.
The js code to get the unique productname from your data would be
const data =[
{
"$class": "org.stock.mynetwork.Commodity",
"time": "1",
"dataType" : "in",
"productName": "A",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "3",
"dataType": "out",
"productName": "A",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "5",
"dataType": "out",
"productName": "B",
"quantity": 1,
"country": "Unknown",
"owner": "BABA"
}
];
const distinctProductName = [...new Set(data.map(x => x.productName))];
You will have your distinct ProductName in distinctProductName but you won't have the details of product. From your questions, it didnot feel necessary too. If you need the details too, you could do something like
const data =[
{
"$class": "org.stock.mynetwork.Commodity",
"time": "1",
"dataType" : "in",
"productName": "A",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "3",
"dataType": "out",
"productName": "A",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "5",
"dataType": "out",
"productName": "B",
"quantity": 1,
"country": "Unknown",
"owner": "BABA"
}
];
const result = Array.from(new Set(data.map(x => x.productName)))
.map(productName => {
return{
productName: productName,
dataType : data.find(x => x.productName ===productName).dataType
//you can obtain other values as I obtained dataType
};
});
You will have your data in result.
Like http.solve and http.solveXpath is there anything similar for JSON.
In the snipped provided, I have to capture complete master node based on condition that the person should be either MD or MD,PhD.
Json syntax with Jmeter: $..[?(#.person_credentials == 'MD' || #.person_credentials == 'MD,PhD')]
Looking for similar thing that can be implemented in OpenScript
Kindly provide ur inputs
Thanks,
Sumit
[{
"person_party_id": 3663409,
"person_party_number": "1199490",
"person_party_name": "Andrea Ward",
"person_obj_version_num": 2,
"person_profile_id": 31036987,
"person_profile_obj_version_num": 2,
"person_name": "Andrea Lee Ward",
"person_pre_name_adjunct": {
"lv_type_id": "TCA|CONTACT_TITLE",
"code": "MS."
},
"person_first_name": "Andrea",
"person_middle_name": "Lee",
"person_last_name": "Ward",
"person_name_suffix": null,
"person_academic_title": null,
"person_credentials": "HT(ASCP)",
"person_ptr_number": null,
"relationship_id": 1760699,
"relationship_obj_version_num": 2,
"relationship_party_id": 3959866,
"relationship_party_number": "52898",
"relationship_party_name": "Andrea Ward-Chester County Hospital-52898-52898-52898-52898",
"relationship_party_obj_version_num": 2,
"org_contact_id": 2006288,
"org_contact_obj_version_num": 2,
"is_generic": false,
"email": {
"contact_point_id": 1170409,
"email_address": "andrea.ward2#uphs.upenn.edu",
"object_version_number": 2
},
"phone": {
"contact_point_id": 1170414,
"phone_country_code": "1",
"phone_area_code": "610",
"phone_number": "431-5561",
"phone_extension": null,
"object_version_number": 2
},
"roles": [{
"lv_type_id": "TCA|CONTACT_ROLE_TYPE",
"code": "ELABUSER"
}, {
"lv_type_id": "TCA|CONTACT_ROLE_TYPE",
"code": "PRIMREL"
}],
"contact_id": 3663409,
"first_name": "Andrea",
"last_name": "Ward",
"credentials": "HT(ASCP)",
"emails": [{
"contact_point_id": 1170409,
"email_address": "andrea.ward2#uphs.upenn.edu",
"related_accounts": null,
"object_version_number": 2
}],
"phone_numbers": [{
"type": "Telephone",
"contact_point_id": 1170414,
"phone_country_code": "1",
"phone_area_code": "610",
"phone_number": "431-5561",
"phone_extension": null,
"related_accounts": null,
"object_version_number": 2
}, {
"type": "Fax",
"contact_point_id": 1170417,
"phone_country_code": "1",
"phone_area_code": "610",
"phone_number": "430-2935",
"phone_extension": null,
"related_accounts": null,
"object_version_number": 3
}]
}, {
"person_party_id": 3620546,
"person_party_number": "208332",
"person_party_name": "James Heald",
"person_obj_version_num": 5,
"person_profile_id": 15949851,
"person_profile_obj_version_num": 2,
"person_name": "James Irwin Heald",
"person_pre_name_adjunct": {
"lv_type_id": "TCA|CONTACT_TITLE",
"code": "DR."
},
"person_first_name": "James",
"person_middle_name": "Irwin",
"person_last_name": "Heald",
"person_name_suffix": null,
"person_academic_title": null,
"person_credentials": "MD,PhD",
"person_ptr_number": null,
"relationship_id": 1728597,
"relationship_obj_version_num": 2,
"relationship_party_id": 3927762,
"relationship_party_number": "18195",
"relationship_party_name": "James Heald-Chester County Hospital-18195-18195-18195-18195",
"relationship_party_obj_version_num": 2,
"org_contact_id": 1971610,
"org_contact_obj_version_num": 2,
"is_generic": false,
"email": {
"contact_point_id": 1167675,
"email_address": "james.heald#uphs.upenn.edu",
"object_version_number": 2
},
"phone": {
"contact_point_id": 1167678,
"phone_country_code": "1",
"phone_area_code": "610",
"phone_number": "431-5471",
"phone_extension": null,
"object_version_number": 2,
"type": "Telephone"
},
"roles": [{
"lv_type_id": "TCA|CONTACT_ROLE_TYPE",
"code": "ELABEDSTADM"
},
{
"lv_type_id": "TCA|CONTACT_ROLE_TYPE",
"code": "ELABSTADM"
},
{
"lv_type_id": "TCA|CONTACT_ROLE_TYPE",
"code": "PRIMREL"
}
],
"contact_id": 3620546,
"first_name": "James",
"last_name": "Heald",
"credentials": "MD,PhD",
"emails": [{
"contact_point_id": 1167675,
"email_address": "james.heald#uphs.upenn.edu",
"related_accounts": null,
"object_version_number": 2
}],
"phone_numbers": [{
"type": "Telephone",
"contact_point_id": 1167678,
"phone_country_code": "1",
"phone_area_code": "610",
"phone_number": "431-5471",
"phone_extension": null,
"related_accounts": null,
"object_version_number": 2
},
{
"type": "Fax",
"contact_point_id": 1167683,
"phone_country_code": "1",
"phone_area_code": "610",
"phone_number": "430-2935",
"phone_extension": null,
"related_accounts": null,
"object_version_number": 2
}
]
}]
I try to use local data with treeGrid, when i click the unfold
button . I get the tips "Cannot read property 'rowIndex' of
undefined"
<!DOCTYPE html>
<html lang="en">
<head>
<!-- The jQuery library is a prerequisite for all jqSuite products -->
<script type="text/ecmascript" src="./js/jquery.min.js"></script>
<!-- This is the Javascript file of jqGrid -->
<script type="text/ecmascript" src="./js/jquery.jqGrid.min.js"></script>
<!-- This is the localization file of the grid controlling messages, labels, etc.
<!-- We support more than 40 localizations -->
<script type="text/ecmascript" src="./js/i18n/grid.locale-en.js"></script>
<!-- A link to a jQuery UI ThemeRoller theme, more than 22 built-in and many more custom -->
<link rel="stylesheet" type="text/css" media="screen" href="./css/jquery-ui-1.10.4.custom.min.css" />
<!-- The link to the CSS that the grid needs -->
<link rel="stylesheet" type="text/css" media="screen" href="./css/ui.jqgrid.css" />
<meta charset="utf-8" />
<title>jqTreeGrid - Load On Demand - Load all Rows at once collapsed</title>
</head>
<body>
<table id="tree"></table>
<div id="pager"></div>
<script type="text/javascript">
var rows = [
{
"category_id": "1",
"name": "ELECTRONICS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "1",
"rgt": "44",
"level": "0",
"uiicon": ""
},
{
"category_id": "2",
"name": "TELEVISIONS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "2",
"rgt": "19",
"level": "1",
"uiicon": ""
},
{
"category_id": "3",
"name": "TUBE",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "3",
"rgt": "8",
"level": "2",
"uiicon": ""
},
{
"category_id": "11",
"name": "26 \" TV",
"price": "200.00",
"qty_onhand": "1",
"color": "black",
"lft": "4",
"rgt": "5",
"level": "3",
"uiicon": "ui-icon-image"
},
{
"category_id": "12",
"name": "30 \" TV",
"price": "350.00",
"qty_onhand": "2",
"color": "black",
"lft": "6",
"rgt": "7",
"level": "3",
"uiicon": "ui-icon-document"
},
{
"category_id": "4",
"name": "LCD",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "9",
"rgt": "12",
"level": "2",
"uiicon": ""
},
{
"category_id": "13",
"name": "Super-LCD 42\" ",
"price": "400.00",
"qty_onhand": "10",
"color": "all",
"lft": "10",
"rgt": "11",
"level": "3",
"uiicon": "ui-icon-video"
},
{
"category_id": "5",
"name": "PLASMA",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "13",
"rgt": "18",
"level": "2",
"uiicon": ""
},
{
"category_id": "14",
"name": "Ultra-Plasma 62\" ",
"price": "440.00",
"qty_onhand": "2",
"color": "silver",
"lft": "14",
"rgt": "15",
"level": "3",
"uiicon": "ui-icon-clipboard"
},
{
"category_id": "15",
"name": "Value Plasma 38\" ",
"price": "312.00",
"qty_onhand": "0",
"color": "silver",
"lft": "16",
"rgt": "17",
"level": "3",
"uiicon": "ui-icon-clipboard"
},
{
"category_id": "6",
"name": "PORTABLE ELECTRONICS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "20",
"rgt": "43",
"level": "1",
"uiicon": ""
},
{
"category_id": "7",
"name": "MP3 PLAYERS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "21",
"rgt": "32",
"level": "2",
"uiicon": ""
},
{
"category_id": "8",
"name": "FLASH",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "22",
"rgt": "29",
"level": "3",
"uiicon": ""
},
{
"category_id": "17",
"name": "Super-Shuffle 1gb",
"price": "20.00",
"qty_onhand": "11",
"color": "all",
"lft": "23",
"rgt": "24",
"level": "4",
"uiicon": "ui-icon-note"
},
{
"category_id": "21",
"name": "5Gb Flash",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "25",
"rgt": "26",
"level": "4",
"uiicon": "ui-icon-comment"
},
{
"category_id": "22",
"name": "10Gb flash ",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "27",
"rgt": "28",
"level": "4",
"uiicon": "ui-icon-tag"
},
{
"category_id": "16",
"name": " Power-MP3 128mb",
"price": "123.00",
"qty_onhand": "2",
"color": "withe",
"lft": "30",
"rgt": "31",
"level": "3",
"uiicon": "ui-icon-signal-diag"
},
{
"category_id": "9",
"name": "CD PLAYERS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "33",
"rgt": "38",
"level": "2",
"uiicon": ""
},
{
"category_id": "18",
"name": " Porta CD ",
"price": "10.00",
"qty_onhand": "0",
"color": "",
"lft": "34",
"rgt": "35",
"level": "3",
"uiicon": "ui-icon-eject"
},
{
"category_id": "19",
"name": "CD To go!",
"price": "110.00",
"qty_onhand": "11",
"color": "",
"lft": "36",
"rgt": "37",
"level": "3",
"uiicon": "ui-icon-power"
},
{
"category_id": "10",
"name": "2 WAY RADIOS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "39",
"rgt": "42",
"level": "2",
"uiicon": ""
},
{
"category_id": "20",
"name": "Family Talk 360 ",
"price": "200.00",
"qty_onhand": "15",
"color": "",
"lft": "40",
"rgt": "41",
"level": "3",
"uiicon": "ui-icon-volume-on"
},
{
"category_id": "23",
"name": "COMPUTERS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "45",
"rgt": "50",
"level": "0",
"uiicon": ""
},
{
"category_id": "25",
"name": "DESKTOP ",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "46",
"rgt": "47",
"level": "1",
"uiicon": ""
},
{
"category_id": "26",
"name": "LAPTOPS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "48",
"rgt": "49",
"level": "1",
"uiicon": ""
},
{
"category_id": "24",
"name": "APPLIANCES",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "51",
"rgt": "52",
"level": "0",
"uiicon": ""
}
]
jQuery(document).ready(function ($) {
jQuery('#tree').jqGrid({
// "url":"data.json",
"colModel": [
{
"name": "category_id",
"index": "accounts.account_id",
"sorttype": "int",
"key": true,
"hidden": true,
"width": 50
}, {
"name": "name",
"index": "name",
"sorttype": "string",
"label": "Name",
"width": 170
}, {
"name": "price",
"index": "price",
"sorttype": "numeric",
"label": "Price",
"width": 90,
"align": "right"
}, {
"name": "qty_onhand",
"index": "qty_onhand",
"sorttype": "int",
"label": "Qty",
"width": 90,
"align": "right"
}, {
"name": "color",
"index": "color",
"sorttype": "string",
"label": "Color",
"width": 100
}, {
"name": "lft",
"hidden": true
}, {
"name": "rgt",
"hidden": true
}, {
"name": "level",
"hidden": true
}, {
"name": "uiicon",
"hidden": true
}
],
"width": "780",
"hoverrows": false,
"viewrecords": false,
"gridview": true,
"height": "auto",
"sortname": "lft",
"loadonce": true,
"rowNum": 100,
"scrollrows": true,
// enable tree grid
"treeGrid": true,
// which column is expandable
"ExpandColumn": "name",
// datatype
"treedatatype": "json",
// the model used
"treeGridModel": "nested",
// configuration of the data comming from server
"treeReader": {
"left_field": "lft",
"right_field": "rgt",
"level_field": "level",
"leaf_field": "isLeaf",
"expanded_field": "expanded",
"loaded": "loaded",
"icon_field": "icon"
},
// "sortorder": "asc",
"datatype": "local",
"data": rows,
"pager": "#pager"
});
});
</script>
</body>
</html>
For localdata type, you must configure the localReader parameter:
localReader: {
repeatitems: true,
// cell: "",
id: "category_id"
}
I am attempting to reference the message array contained within this JSON but cant seem to get it.
[
{
"MessageThreadID": 1,
"CustomerID": 171,
"MessageType": 1,
"Subject": "Test Message",
"OpenDate": "2015-09-17T00:00:00",
"Closed": false,
"ClosedDate": null,
"Messages": [
{
"IBMessageID": 1,
"MessageThreadID": 1,
"MessageText": "Test Message",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-17T17:23:00"
},
{
"IBMessageID": 2,
"MessageThreadID": 1,
"MessageText": "Test this Update",
"FromCustomer": false,
"UserID": 1,
"Date": "2015-09-17T17:23:00"
},
{
"IBMessageID": 3,
"MessageThreadID": 1,
"MessageText": "My New Message",
"FromCustomer": false,
"UserID": 1,
"Date": "2015-09-17T17:23:00"
},
{
"IBMessageID": 4,
"MessageThreadID": 1,
"MessageText": "Reply",
"FromCustomer": false,
"UserID": 1,
"Date": "2015-09-17T17:05:00"
},
{
"IBMessageID": 5,
"MessageThreadID": 1,
"MessageText": "Some sensible shit",
"FromCustomer": false,
"UserID": 1,
"Date": "2015-09-17T17:23:00"
},
{
"IBMessageID": 14,
"MessageThreadID": 1,
"MessageText": "Message 2",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-21T14:10:00"
},
{
"IBMessageID": 16,
"MessageThreadID": 1,
"MessageText": "Message 2",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-22T16:22:00"
},
{
"IBMessageID": 25,
"MessageThreadID": 1,
"MessageText": "Added via abacus\r\n",
"FromCustomer": false,
"UserID": 1,
"Date": "2015-09-22T16:22:00"
},
{
"IBMessageID": 26,
"MessageThreadID": 1,
"MessageText": "sdsdsdsd",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-22T16:40:00"
},
{
"IBMessageID": 27,
"MessageThreadID": 1,
"MessageText": "test",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-22T17:02:00"
},
{
"IBMessageID": 28,
"MessageThreadID": 1,
"MessageText": "test",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-22T17:06:00"
}
]
}
]
The furthest I have gotten is:
#cleanmessages = JSON.parse([#messages].to_json).first
This returns:
[
{
"IBMessageID" =>1,
"MessageThreadID" =>1,
"MessageText" =>"Test Message",
"FromCustomer" =>true,
"UserID" =>nil,
"Date" =>"2015-09-17T17:23:00 "},
{
"IBMessageID"=>2,
"MessageThreadID" =>1,
"MessageText" =>"Test this Update",
"FromCustomer" =>false,
"UserID" =>1,
"Date" =>"2015-09-17T17:23:00 "},
{
"IBMessageID"=>3,
"MessageThreadID" =>1,
"MessageText" =>"My New Message",
"FromCustomer" =>false,
"UserID" =>1,
"Date" =>"2015-09-17T17:23:00 "},
{
"IBMessageID"=>4,
"MessageThreadID" =>1,
"MessageText" =>"Reply",
"FromCustomer" =>false,
"UserID" =>1,
"Date" =>"2015-09-17T17:05:00 "},
{
"IBMessageID"=>5,
"MessageThreadID" =>1,
"MessageText" =>"Some sensible shit",
"FromCustomer" =>false,
"UserID" =>1,
"Date" =>"2015-09-17T17:23:00 "},
{
"IBMessageID"=>14,
"MessageThreadID" =>1,
"MessageText" =>"Message 2",
"FromCustomer" =>true,
"UserID" =>nil,
"Date" =>"2015-09-21T14:10:00 "},
{
"IBMessageID"=>16,
"MessageThreadID" =>1,
"MessageText" =>"Message 2",
"FromCustomer" =>true,
"UserID" =>nil,
"Date" =>"2015-09-22T16:22:00 "},
{
"IBMessageID"=>25,
"MessageThreadID" =>1,
"MessageText" =>"Added via abacus\r\n",
"FromCustomer" =>false,
"UserID" =>1,
"Date" =>"2015-09-22T16:22:00 "},
{
"IBMessageID"=>26,
"MessageThreadID" =>1,
"MessageText" =>"sdsdsdsd",
"FromCustomer" =>true,
"UserID" =>nil,
"Date" =>"2015-09-22T16:40:00 "},
{
"IBMessageID"=>27,
"MessageThreadID" =>1,
"MessageText" =>"test",
"FromCustomer" =>true,
"UserID" =>nil,
"Date" =>"2015-09-22T17:02:00 "},
{
"IBMessageID"=>28,
"MessageThreadID" =>1,
"MessageText" =>"test",
"FromCustomer" =>true,
"UserID" =>nil,
"Date" =>"2015-09-22T17:06:00
}
]
How do i reference each element within each array. I want to in the end be able to display each element sepearately for each thread.
This will give you a hash with the message grouped by the MessageThreadID
#cleanmessages.group_by{ |msg_hash| msg_hash['MessageThreadID']}
Not clear what output you actually want.
document = JSON.parse([#messages].to_json)
document.each do |thread|
# gives you all text messages within the current thread: "Test Message", "Test this Update", ...
messages = thread['Messages'].map {|m| m['MessageText']}
# iterates over all messages in the current thread
thread['Messages'].each {|m| puts "Message #{m['IBMessageID']} from thread #{thread['MessageThreadID']}"}
end
In general JSON.parse doesn't return something special. It returns either Array for corresponding JSON array or Hash for corresponding JSON object.
Is it possible to find a string in a list for a gets.chomp for if, elsif?
def number_problem()
puts "Please enter a number 1-100 for a phrase or word back!"
number = gets.chomp
mult_of_three = ["3", "6", "9", "12", "15", "18", "21", "24", "27", "30", "33", "36", "39", "42", "45", "48", "51", "54", "57", "60", "63", "66", "69", "72", "75", "78", "81", "84", "87", "90", "93", "96", "99"]
mult_of_five = ["5", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55", "60", "65", "70", "75", "80", "85", "90", "95", "100"]
mult_of_both = ["15", "30", "45", "60", "75", "90"]
if number == mult_of_three
puts "Bit"
To find if an element is present in an Array, you can use include?. Here:
if mult_of_three.include?(number)
Example:
number = gets.chomp
36
# => "36"
mult_of_three.include?(number)
# => true
number = gets.chomp
98
# => "98"
mult_of_three.include?(number)
# => false