How to display JSON data in DisplayActionSheet Xamarin? - json

I would like to know how to fetch JSON data from a URL and display it in a DisplayActionSheet? I don't want to hardcode the data.
JSON Data
[
{
"id": 284,
"name": "Complete Task"
},
{
"id": 285,
"name": "Uncomplete Task"
},
{
"id": 286,
"name": "Close Task"
}
]
So far I have something like this, but I'm not sure if I'm doing it right:
private const string TasksMenuUrl = "json-data-url-goes-here";
private ObservableCollection<TaskMenuOptions> _taskMenu;
private HttpClient _client = new HttpClient();
private async void FetchMenu()
{
var menuContent = await _client.GetStringAsync(TasksMenuUrl);
var taskMenu = JsonConvert.DeserializeObject<List<TaskMenuOptions>>(menuContent);
_taskMenu = new ObservableCollection<TaskMenuOptions>(taskMenu);
//var action = await DisplayActionSheet("Task Actions", "Cancel", null, "Complete Task", "Uncomplete Task", "Close Task");
}
Thank you.

the last argument in DisplayActionSheet is a string[]
var taskMenu = JsonConvert.DeserializeObject<List<TaskMenuOptions>>(menuContent);
var options = taskMenu.Select(t => t.Name).ToArray<string>();
var action = await DisplayActionSheet("Task Actions", "Cancel", null, options);

Related

How to concatenate / append a JsonObject to a JsonArray

Given the following code which gets called variously in the code, and then gets written to a file (separate method), I simple need to append the next value into the array. This is probably a simple Collections question but wasn't sure if there was a specialized Gson way of doing this:
public static void metricAsJSON(String testName, long testTime) {
Date date = new Date();
Timestamp ts = new Timestamp(date.getTime());
JSONObject obj = new JSONObject();
obj.put("testname", testName);
obj.put("Duration", testTime);
obj.put("Timestamp", ts.toString());
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(obj.toJSONString());
jsoncontent = gson.toJson(je);
JsonArray jsonArray = new JsonArray();
jsonArray.add(je);
jsoncontent = gson.toJson(jsonArray);
}
public static JsonArray appendMetricAsJson(String jsoncontent){
JsonArray jsonContentList = new JsonArray();
jsonContentList.add(jsoncontent);
return jsonContentList;
FileWriter fw1 = new FileWriter(f1, true);
PrintWriter pw1 = new PrintWriter(fw1);
if (f1.exists() && f1.isFile()) {
pw1.println(jsonContentList);
pw1.flush();
pw1.close();
fw1.close();
}
}
Current output:
[
{
"Duration": 30,
"testname": "Shopping link click to dropdown display time:",
"Timestamp": "2019-10-15 09:47:53.804"
}
]
Desired output:
[
{
"Duration": 30,
"testname": "Shopping link click to dropdown display time:",
"Timestamp": "2019-10-15 09:45:13.334"
},
{
"Duration": 16,
"testname": "Clothing link click to dropdown display time",
"Timestamp": "2019-10-15 09:44:34.356"
},
{
"Duration": 24,
"testname": "Toys link click to dropdown display time",
"Timestamp": "2019-10-15 09:46:33.453"
},
{
"Duration": 34,
"testname": "Electrics link click to dropdown display time",
"Timestamp": "2019-10-15 09:47:53.566"
}
]

How to convert JSON/Dynamic Structure to Map in Haxe?

I'm a very new to coding and generally work with a drag and drop editor that is based on Haxe (Stencyl) as a hobby.
I have a JSON file that I would like to convert into a nested map (dictionary). I have tried using the JSON parsing function but it returns an anonymous (dynamic) structure.
How can I either convert the JSON file into a map or convert the Anonymous structure into a map?
Sample of the JSON:
{
"apple": {
"value": 10,
"health": 15,
"tags": [
"fruit",
"fiber",
"sweet"
]
},
"lemon": {
"value": 5,
"health": 10,
"tags": [
"fruit",
"citrus",
"sour"
]
},
"ham": {
"value": 50,
"health": 50,
"tags": [
"salty",
"meat"
]
}
}
Another option would be to use the json2object library, which natively supports Map<String, T>:
import sys.io.File;
import json2object.JsonParser;
class Main {
public static function main() {
var parser = new JsonParser<Data>();
var source = File.getContent("data.json");
var data = parser.fromJson(source, "data.json");
trace(data["apple"].value); // 10
}
}
typedef Data = Map<String, {
var value:Int;
var health:Int;
var tags:Array<String>;
}>
This approach avoids both reflection and Dynamic, which are usually considered bad practice.
You could create Map and fill it via Reflect api:
var parse = haxe.Json.parse(s);
var map:Map<String, StructData> = new Map();
for(field in Reflect.fields(parse))
{
map.set(field, Reflect.field(parse, field));
}
typedef StructData = {
var value:Int;
var health:Int;
var tags:Array<String>;
}
https://try.haxe.org/#DFa77
Take a look at the DynamicAccess abstract here.
Given your sample I've made a quick example here:
import haxe.DynamicAccess;
typedef Food = {
var value:Int;
var health:Int;
var tags:Array<String>;
}
class Test {
static function main() {
var json = {
"apple": {
"value": 10,
"health": 15,
"tags": [
"fruit",
"fiber",
"sweet"
]
},
"lemon": {
"value": 5,
"health": 10,
"tags": [
"fruit",
"citrus",
"sour"
]
},
"ham": {
"value": 50,
"health": 50,
"tags": [
"salty",
"meat"
]
}
};
var foodMap:DynamicAccess<Food> = json;
// Get a single entry
var apple = foodMap.get("apple");
trace(apple.tags.join(", "));
// Loop through names
for (foodName in foodMap.keys()) {
trace(foodName);
trace(foodMap.get(foodName).value);
}
}
}

Not reading the second data from json

I am trying to read data from json (2 cycles) and add an entry. the first time, the data added successfully but during the second cycle, it failed. below are the details. Also working when run with page object and without reading from json;
The "test_pom_fromjson" added successfully but when running for the test_pom_fromjson_first it failed with error saying no element found using element(by.id('s2id_autogen10')).click(); which is the locator for location column.
Refer the screenshot and html code for the page - https://pastebin.com/ZXyRx1tv
mo.ts: pageobject for the below spec file
var mo = function () {
this.createbutton = function () {
var createb = element(by.css('button[title="Add Master Obligation"]'))
//var createbutton = element(by.buttonText('↵ ↵ Add Master Obligatio...↵ '))
browser.executeScript("arguments[0].scrollIntoView();arguments[1].click();", createb, createb);
}
this.MasterObligationName = function (value) {
element(by.model('obligation.ObligationName')).sendKeys(value);
}
this.Module = function (value) {
element(by.id('s2id_TaxProcess')).click();
element.all(by.repeater('item in obligation.TaxProcess.list')).get(value).click();
}
this.Location = function (value) {
element(by.id('s2id_autogen10')).click();
element.all(by.repeater('item in obligation.Jurisdiction.list')).get(value).click();
}
this.CentralObligation = function (value) {
element.all(by.model('obligation.CentralObligation')).get(value).click();
}
this.Type = function (value) {
element(by.id('s2id_txtReturnType')).click();
element.all(by.repeater('item in obligation.ReturnType.list')).get(value).click();
}
this.Years = function (value) {
element(by.id('s2id_txtTaxYear')).click();
element.all(by.repeater('item in obligation.TaxYears.list')).get(value).click();
}
this.Periods = function (value) {
element(by.id('s2id_txtPeriod')).click();
element.all(by.repeater('tem in obligation.Periods.list')).get(value).click();
}
this.Forms = function (value) {
element.all(by.id('s2id_txtForms')).get(0).click();
element.all(by.repeater('item in obligation.Forms.list')).get(value).click();
}
this.Reports = function (value) {
element.all(by.id('s2id_txtForms')).get(1).click();
element.all(by.repeater('item in obligation.Reports.list')).get(value).click();
}
this.savebutton = function () {
var saveb = element(by.css('button[title="Save"]'))
browser.executeScript("arguments[0].scrollIntoView();arguments[1].click();", saveb, saveb);
}
module.exports = new mo();
spec.ts: var mo = require("../page/mo.ts")
var testData = require('../testdata/testdata.json');
testData.forEach(function (data) {
it('create and save master obligation', function () {
browser.sleep(10000);
mo.createbutton();
browser.sleep(10000);
mo.MasterObligationName(data.Master_Obligation_Name)
mo.Module(data.Module);
mo.Location(data.Location);
mo.CentralObligation(data.Central_Obligation);
mo.Type(data.Type);
mo.Years(data.Years);
mo.Periods(data.Periods);
mo.Forms(data.Forms);
mo.Reports(data.Reports);
mo.savebutton();
})
})
testdata.json:
[{
"Master_Obligation_Name": "test_pom_fromjson",
"Module": "2",
"Location": "1",
"Central_Obligation": "0",
"Type": "2",
"Years": "1",
"Periods": "1",
"Forms": "1",
"Reports": "0"
},
{
"Master_Obligation_Name": "test_pom_fromjson_first",
"Module": "2",
"Location": "1",
"Central_Obligation": "0",
"Type": "2",
"Years": "1",
"Periods": "1",
"Forms": "1",
"Reports": "0"
}]
Two options you can try:
1.Wait for some time before that click
2.Either id number is auto generated so it might be changed everytime, so use some other locator.
If the id is auto-generated then look for some other locator then:
element.all(by.id('s2id_Jurisdiction')).get(1).click();
element.all(by.repeater('item in obligation.Jurisdiction.list')).get(value).click();
OR you can also try the following way:
var module = element(by.xpath("//input[starts-with(#id, 's2id_autogen')]")).get(1).click();
var location= element(by.xpath("//input[starts-with(#id, 's2id_autogen')]")).get(2).click();
var type = element(by.xpath("//input[starts-with(#id, 's2id_autogen')]")).get(3).click();

How to receive a json string after a jquery post in WebMatrix?

If I have the following post call:
$('#json_form').submit(function (event) {
event.preventDefault();
var url = $(this).attr('action');
var datos = {
"uno": "lalala",
"dos": "jojojo"
}
var data = JSON.stringify(datos);
$.post(url, data, function (resultado) {
$('#posted_values').html(resultado);
});
});
How can I receive and process the json object in a cshtml file? I mean what I put in Decode call:
if (IsPost)
{
var json_object = Json.Decode(Request???);
}
Edited to complete the answer of #MikeBrind, to help others with the same problem.
Example of using decode for a more complex json object.
$('#json_form').submit(function (event) {
event.preventDefault();
var url = $(this).attr('action');
var datos = {
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumber": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
var data = JSON.stringify(datos);
$.post(url, {"person": data}, function (resultado) {
$('#posted_values').html(resultado);
});
});
Receiving and using:
#{
dynamic json_object;
if (IsPost)
{
json_object = Json.Decode(Request["person"]);
#json_object.firstName<br/>
#json_object.lastName<br/>
#json_object.address.city<br/>
#json_object.address.postalCode<br/>
foreach (dynamic phone in json_object.phoneNumber)
{
#phone.type<br/>
#phone.number
}
}
}
Don't JSON.stringify the data if it is just key/value pairs like this. Do a form post:
$('#json_form').submit(function (event) {
event.preventDefault();
var url = $(this).attr('action');
var datos = {
"uno": "lalala",
"dos": "jojojo"
}
//var data = JSON.stringify(datos); no need for this
$.post(url, datos, function (resultado) {
$('#posted_values').html(resultado);
});
});
Then the values are available from Request["uno"] and Request["dos"]
If you ever do need to use JSON.stringify (which you would for more complex data structures), the JSON is transmitted in the Request body, so you need to extract it from Request.InputStream:
var reader = new StreamReader(Request.InputStream);
var json = reader.ReadToEnd();

Passing Json from action to view by ViewBag

I'm trying to get the result below using JsonResult, but I can't
var localJSON = [
{ "id": "1", "label": "tagName1", "value": "tagValue1" },
{ "id": "2", "label": "tagName2", "value": "tagValue2" },
{ "id": "3", "label": "tagName3", "value": "tagValue3" },
{ "id": "1553", "label": "tagName1553", "value": "tagValue1553" }
];
Here is the way I use:
controller
private JsonResult GetAvailableTags()
{
var tagsList = Facade.Tags.Get(CurrentLocale.ID);
var retValue = new
{
id = tagsList.Select(x => x.ID).ToArray(),
label = tagsList.Select(x => x.Name).ToArray(),
value = tagsList.Select(x => x.Name).ToArray()
};
return Json(retValue);
}
public ActionResult AddPhoto()
{
var availblableTags = GetAvailableTags();
JavaScriptSerializer serializer = new JavaScriptSerializer();
ViewBag.AvailableTags = serializer.Serialize(availblableTags.Data);
return View();
}
view
var localJSON = [ #Html.Raw(ViewBag.AvailableTags)];
The result is
var localJSON = [
{"id":[1,2,3,1553],"label":["tagName1","tagName2","tagName3","tagName1553" ],"value":["tagName1","tagName2","tagName3","tagName1553" ]}
];
What should I do to resolve that?
I assume you want to get x.Value for value in JSON? Then change your assignment for retValue to
var retValue = tagsList.Select(
x => new
{
id = x.Id,
label = x.Name,
value = x.Value
}).ToArray();
In your retValue assignment code you were creating a single object of anonymous type with array-typed members id, label and value. For the output you want you need to create an array, each member of which is an object with simple fields id, name and value.