ReactJS doesn't recognize a value in an object in an array - json

I am making a web app with express + react and I'm sending JSON data.
I am fetching this data with axios and using setState to set the messages variable to response.data.messages
{
"messages":{
"message":{
"username":"Khigoris"
}
}
}
and it doesn't let me do this
<p>{messages.message.username}</p>
But it says it is undefined
I am new to using JSON so I think it's the syntax but I need help.

It looks like the messages object is wrapped in another object, i.e your code looks like:
const value = {
"messages":{
"message":{
"username":"Khigoris"
}
}
}
So you should use:
<p>{value.messages.message.username}</p>

Related

gRPC JSON transcoding : using repeated fields in query parameters

I'm trying to make a request via Swagger using JSON transcoding, but I'm encountering an error when I use a repeated field parameter in my request. The error message I'm receiving is:
response
I've attached my code snippet below for reference. Can someone help me figure out what's causing this issue and provide a solution for making a request with a repeated field parameter using JSON transcoding in Swagger?
Here's my code snippet:
syntax = "proto3";
import "google/api/annotations.proto";
package greet;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {
option (google.api.http) = {
get: "/v1/greeter/{name}/{ids}"
};
}
}
message HelloRequest {
string name = 1;
IdsRequest ids = 2;
}
message HelloReply {
string message = 1;
}
message IdsRequest {
string id = 1;
}
My request:
request
Note: I am using a very simple example just to shorten the code for this question.

How to save a JSON-object and use it in another request?

i'm currently trying to set up some JMeter testplans. I am pretty new to this (started a week ago) and don't have much programming experience either, so i hope you could help me in this case.
I already set up some http requests and have some simple JSON Extractor post processors to save some of the variables and use them with the "${variable}" notation.
But now i need to save and modify an object from a response to use that in the next http request.
My respose is a extremely big JSON object and the part im interested in looks something like this:
{
"payload": {
"workspace": {
"resultInstance": [
[{"part": "1"...}],
[{"part": "2"...}],
...
[{"part": "20"...}]
]
}
}
}
Note that for whatever reason these objects {"part":"1"...} are inside a nested array. And they are also pretty big.
I would like to safe those objects in a variable to use them in my next http request which should looks like this:
{
"instanceChange": {
"functionChecks": [
{"part": "1"...},
{"part": "2"...},
...
{"part": "20"...}
]
}
}
So what im really trying to find is a way to save all of the possible objects inside the nested array "resultInstance" and put them inside the non nested array "functionChecks".
I already looked inside the JMeter documentation but because of my poor programming background i cant find a way to realize this.
I think i need something like the JSR223 PostProcessor and "simply go through the resultInstance-array and use smth. like an getObject() on these", but i cant figure out the code i need and if its even possible to safe objects in variables in Jmeter.
Im pretty thankful for every bit of help or advice :).
Thanks in advance,
aiksn
Add JSR223 PostProcessor as a child of the request which returns the JSON response
Put the following code into "Script" area:
def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())
def request = ['instanceChange': ['functionChecks': response.payload.workspace.resultInstance]]
vars.put('request', new groovy.json.JsonBuilder(request).toPrettyString())
That's it, you should be able to refer the generated request body as ${request} where required
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It
let response ={
"payload": {
"workspace": {
"resultInstance": [
[{"part": "1"...}],
[{"part": "2"...}],
...
[{"part": "20"...}]
]
}
}
};
let requestObj={
"instanceChange": {
"functionChecks": [
]
}
};
response.payload.workspace.resultInstance.forEach(myFunction);
myFunction(item, index) {
requestObj.instance.functionsCheck.push(item[0]);
}

How to handle json in angular4

When I work with a partner to write a website, the backend returns the json format as follows. My front end uses angular4 but I don't know how to use angular4 to process the following json format data. It is a bit worse and there is no json.parse method.
{
"pageNum":1,
"pageSize":4,
"size":1,
"startRow":1,
"endRow":1,
"total":1,
"pages":1,
"list":[
{
"owner":"aa",
"zh_name":"武世伟:广佛06.19-24搭乘ID(正确)",
"push_status":1,
"business":1,
"create_time":1514256602000,
"coupon_status":1,
"monitor_status":1,
"model_status":3,
"message_status":1,
"monitor_end_time":1514256602000,
"random_group":1,
"name":"p_pm_passenger_taxi_20171226105028119",
"calc_way":1,
"monitor_start_time":1514256602000,
"id":11,
"model_num":8837
}
],
"prePage":0,
"nextPage":0,
"isFirstPage":true,
"isLastPage":true,
"hasPreviousPage":false,
"hasNextPage":false,
"navigatePages":8,
"navigatepageNums":[
1
],
"navigateFirstPage":1,
"navigateLastPage":1,
"status":200,//状态200正常,400 错误
"firstPage":1,
"lastPage":1
}
Angular indeed provides you with a JSON.parse()-method which actually derives from JavaScript. You can convert your server response json into an object.
// in your component
private object;
Then you can access the content. For example:
// call this method after having received your json
private show(data: any): void {
this.object = JSON.parse(data);
console.log(this.object.pageNum);
console.log(this.object.total);
console.log(this.object.list[0].zh_name);
}

How do I convert json into a kotlin object in ktor using content negotiation?

I'm new to Ktor, and I'm trying to build a backend that processes login credentials. I'm attempting to use the content negotiation feature to convert JSON into a native Kotlin object, but I keep getting an unsupported media type exception. Here is my code:
fun Application.main() {
install(CallLogging)
install(DefaultHeaders)
install(ContentNegotiation) {
register(ContentType.Application.Json, GsonConverter())
}
routing {
get("/") {
call.respondHtml {
head {
title("Kotlin Webapp")
script {
type = ScriptType.textJScript
src = "main.bundle.js"
}
}
body {
div {
id = "root"
}
}
}
}
post("/login") {
val credentials = call.receive<Credentials>()
println(credentials)
}
}
}
data class Credentials(val username: String, val password: String)
And here is the incoming Json I am trying to convert, which I am sending via XMLHttpRequest:
{"username":"Jamdan2","password":"sometext"}
I have searched the web for answers, but could not find what I am doing wrong. Can anybody help?
For completeness, since my comment seems to have helped:
You need to be sure that the request's Content-Type header is set to the correct value, in this case, application/json, otherwise the server cannot be entirely sure what to do with the received content.
In requests, (such as POST or PUT), the client tells the server what type of data is actually sent.
xhr.setRequestHeader("Content-Type", "application/json");

res.render - passing parameters dynamically

I'm trying to figure out how to render dynamically depending on a number of parameters in an object. Let's create a JSON example
var json =
[
{
"param_1":11,
"param_2":22
},
{
"param_1":33,
"param_2":44
}
]
And here is how I would pass the parameters if var json was static:
res.render("template.hbs", {
item_0_param_1: json[0].param_1
item_0_param_2: json[0].param_2,
item_1_param_1: json[1].param_1
item_1_param_2: json[1].param_2
}
The question that I have is - how can I pass parameters from variable 'json' if it is not static? I was thinking about doing a loop inside render, but I didn't know how would I return assigned parameters. Would appreciate the help. Thanks!