I am using EXT JS 4.1.1.
I have a json response like this:
{ values: ["A","B","C"] }
I then have a model like so:
Ext4.define('model', {
extends: 'Ext4.data.model',
fields: [ 'name' ]
});
I am trying to create models with one value, name, that corresponds to the above json values so that I can use the name values in a ComboBox.
Ext4.define('store',{
extend: 'Ext4.data.Store',
model: 'model',
requires: 'model',
proxy: {
type: 'memory',
reader: {
type: 'json',
root: 'values',
successProperty: 'success'
}
}
});
The issue I'm having is that when populating my ComboBox with the name attribute, it is empty (""). The raw value is correct, it corresponds directly to the appropriate value.
How can I map this array of values correctly to a name field on my model model?
That is not actually a proper JSON response, paste it into http://jsonlint.com/ and you will see for yourself.
Instead your JSON should look like this:
{
"values": [
{
"name": "A"
},
{
"name": "B"
},
{
"name": "C"
}
]
}
Which you will see is valid if you paste into the jsonlint link above.
The store reader is expecting to see 'name' attributes in the data, but cannot, which is why you are seeing this behaviour.
Related
My angular service returns the following type of response:
{
"name": "abc",
"id": 1,
"list": [
{
"name": "listName1",
"id": 1
},
{
"name": "listName2",
"id": 2
}
]
}
This is what the call in the service looks like:
fetchX(): Observable<X> {
return this.http.get<X>(some_url)
}
Now, in my component I can access the 'name' and 'id' attribute of the returned object, but for the 'list', which is a list itself, I only get 'undefined'. I'm displaying the list elements in my application (by their name), and they don't actually show up there either (the HTML part for this is definitely correct, this isn't the issue).
myX: CustomX
myList: CustomList[]
ngOnInit() {
this.service.fetchX().subscribe((response) => {
this.myX = response,
this.myList = response.list,
console.log(response.name), //returns 'abc'
console.log(response.id), //returns '1'
console.log(response.list) //returns 'undefined'})}
The entities look like this:
export class CustomX {
name: string
id: number
list: CustomList[]
}
class CustomList {
name: string
id: number
}
What am I missing? I think I may be handling the response in the service incorrectly.
Can someone help me with the following JSON format.
deals: {
'obj-1': { id: '1', name: 'a', text: 'text' },
'obj-2': { id: '2', name: 'b', text: 'text' }
}
I'm doing a tutorial step by step and found this type of JSON, but I have doubts on how to reproduce it.
JSON
I understood that it is an object with several other objects.
bringing from my backend this would seriously list objects like that.
{
"deals": [
{
"id": "1",
"name": "a",
"text": "text"
},
{
"id": "2",
"name": "b",
"text": "text"
}
]
}
starting now with react and I'm trying to understand a lot.
can someone help me to reproduce in this way or even explain a little more about this model?
create an object with several objects and name each one!
QUESTION: how to convert from the format that my backend returns me to that different format?
Using forEach on backend.deals, you can extract each of the deals and assign it to data as an object:
const data = { deals: {}};
backend.deals.forEach(deal => { data.deals[`obj-${deal.id}`] = deal });
console.log(data);
I have the following rswag test:
response '200', 'response with array of objects' do
schema type: :array,
items: {
type: :object,
properties: {
expected_id: { type: "integer", format: "int32" },
}
}
run_test!
end
My API responds with JSON:
[
"invalid_key": 1,
"invalid_key": 2
]
In other words, an invalid response according to the schema above.
However, the test does not fail. This is unexpected.
On the other hand, if my API responds with an array of null values: "[null]" I get the response I expect:
Failure/Error: test.run
Rswag::Specs::UnexpectedResponse:
Expected response body to match schema: The property '#/0' of type null did not match the following type: object in schema 19e6c168-1da4-58a6-93fc-a33d9d91233a
So my test is checking that there is an array of objects, but not checking that the properties in the nested objects are what is expected. How do I make it check the properties as well?
I was missing a required clause in my schema:
schema type: :array,
items: {
type: :object,
properties: {
expected_id: { type: "integer", format: "int32" },
}
required: ['expected_id'] # <- this was missing
}
This raises a test error if the response my endpoint generates does not include the expected_id key.
I'm create a web service using Web API and fsharp. From the web service I am returning data from a query. (Note that I am using a custom query result type and not one of the standard .Net types, like datatable etc.) That data could be many different "shapes" (i.e. different number of columns etc etc).
Some sample json I'm looking to create would be, e.g.
{
columns :
[
{ field: "FirstName", title: "First" },
{ field: "LastName", title: "Last" },
{ field: "EmailAddress", title: "Email" },
{ field: "Other", title: "Other" },
],
data :
[
{ FirstName: "A", LastName: "B", EmailAddress: "a#b.c", Other: "zzz" },
{ FirstName: "C", LastName: "D", EmailAddress: "c#d.c", Other: "zzz" },
{ FirstName: "E", LastName: "F", EmailAddress: "e#f.c", Other: "zzz" }
]
}
but as I say the number of columns will be vary as will their names, data types etc etc.
So far these are my types.
[<CLIMutable>]
[<JsonObject(MemberSerialization=MemberSerialization.OptOut)>]
type QueryColumn = {
field: string
title: string
}
[<CLIMutable>]
[<JsonObject(MemberSerialization=MemberSerialization.OptOut)>]
type QueryData = {
columns: IEnumerable<QueryColumn>
data: IEnumerable<IEnumerable<string * string>>
}
The above type successfully produces the correct structure of json for the columns element, but not data. I am wondering how I could best structure the "data" type to produce the json structure I am after. I am beginning to wonder whether I may need a type provider so that I can dynamically work out the structure of the query results.
Any suggestions would be gratefully received.
Thx
S
On Leafs suggestion (see comments above), I changed the type definition for QueryData to
[<CLIMutable>]
[<JsonObject(MemberSerialization=MemberSerialization.OptOut)>]
type QueryData = {
columns: IEnumerable<QueryColumn>
data: IEnumerable<IDictionary<string,string>>
}
and it works fine.
Lets say i had a json format like this
$(document).ready(function ()
{
$('#grid').kendoGrid(
{
scrollable: false,
dataSource: {data: [{ test: 1, data: [ { "TestHeader": "This is some test data"} ] }]},
columns: [ { field: 'TestHeader' } ]
});
});
how do i get the testHeader field to display on kendoui grid?
if the json data is like this
{ test: 1, data: [ { "TestHeader": "This is some test data"} ] }
it would work, but not if it is nested like this
{data: [{ test: 1, data: [ { "TestHeader": "This is some test data"} ] }]}
I can't control what server return, so i can't change the return json, so how can i get this done?
I got it done already, using schema: parse on datasource
found it at here How can I use nested Json to populate Kendo UI grid?