Create JSON with wrapped name in javascript? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
For example i have JSON like this
{
"1":"12",
"2":"13",
"3":"14"
}
How can i wrap them like
"data":{
"1":"12",
"2":"13",
"3":"14"
}
Any lib we can do this ?
Thank you a lots

const json = `{
"1": "34.4",
"dog":"Snoopy",
"more": [ 1, 2, 3, 4 ]
}`
const myObj = {
data: JSON.parse(json)
}
console.log(myObj);
Simply that would do it. JSON is simply a string format, representing a javascript object. So within myObj, I can assign that to an object property and parse that JSON string into an object.

Related

What is the parenthesis in a json? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
An API call returns a json looking like below:
{
"applicationData": {
"outerMap": {
"record(id=1, description=description)": {
"key(id=1, englishDescription=enDescription)": [
{
"id": 1,
"description": "point 1"
},
{
"id": 2,
"description": "point 2"
}
]
}
}
},
"messages": [
"successfully"
],
"httpStatus": "OK"
}
The return type of the API call is:
public class Data {
private Map<Record, Map<Key, List<Point>>> outerMap;
}
I don't remember parenthesis is used in json, so quite confused about it. Seesm it could be used to "de-serialize" to restore the object?
Every valid string can be a valid key in JSON. So parentheses in key names are possible.
It is up to the application to interpret the content of the parentheses.

Not able to Parse a JSON [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am getting object from back end and in front end I am parsing JSON object but the result I am getting object object.
Here is my Code.
JSON (data contains the following JSON)
{
"sc_sub":"ab",
"sc_sub1":"abc"
}
var lclObj = JSON.parse(data);
var a = lclObj[0].sc_sub;
I made changes to object as array, Now the Problem is I am sending one by one values as array from back end I am getting two arrays as
[{
"sc_sub":"ab",
"sc_sub1":"abc"
}]
[{
"sc_sub":"ab",
"sc_sub1":"abc"
}]
How to remove previous array and set new one?
You can use this code:
var useremail = #Html.Raw(Json.Encode(Data))

Best way to parse this string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
data:
{\"id\":123,\"channel\":\"private_A25BHd\",\"text\":{\"content\":\"{\\"m\\":\\"event\\",\\"p\\":{\\"id\\":123123123,\\"aid\\":125123123,\\"sym\\":\\"BITMEX:XBTUSD\\",\\"res\\":\\"1\\",\\"desc\\":\\"test
message on line1\\ntest message on
line2\\",\\"snd\\":false,\\"snd_file\\":\\"alert\/fired\\",\\"snd_duration\\":0.0,\\"popup\\":true,\\"fire_time\\":123123,\\"bar_time\\":123123,\\"cross_int\\":true}}\",\"channel\":\"alert\"}}
It unfortunately comes in as a string, though I'd like it to change into a dict if possible.
use json library in python
import json
json.loads(your_string)
You can use the function json.loads() which parse the string in to JSON object(Python dictionary) by importing the json library.
Code :
import json
json_string = '{\"id\":123,\"channel\":\"private_A25BHd\",\"text\":{\"content\":\"{\\"m\\":\\"event\\",\\"p\\":{\\"id\\":123123123,\\"aid\\":125123123,\\"sym\\":\\"BITMEX:XBTUSD\\",\\"res\\":\\"1\\",\\"desc\\":\\"test message on line1\\ntest message on line2\\",\\"snd\\":false,\\"snd_file\\":\\"alert\/fired\\",\\"snd_duration\\":0.0,\\"popup\\":true,\\"fire_time\\":123123,\\"bar_time\\":123123,\\"cross_int\\":true}}\",\"channel\":\"alert\"}}'
parsed_string = json.loads(json_string)
print(parsed_string)
Output :
{'text': {'channel': 'alert', 'content': '{"m":"event","p":{"id":123123123,"aid":125123123,"sym":"BITMEX:XBTUSD","res":
"1","desc":"test message on line1\ntest message on line2","snd":false,"snd_file":"alert/fired","snd_duration":0.0,"popu
p":true,"fire_time":123123,"bar_time":123123,"cross_int":true}}'}, 'channel': 'private_A25BHd', 'id': 123}
& if you want to simply print any specific value, you can use
print(parsed_string["id"])
it prints the id value which is 123

How to loop over a nested JavaScript object in jQuery? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am new to Javascript and jQuery. I am trying to create something like this (object) below and read the keys and retrieve the values.
var members = {unit:1,name: ["AA", "AB"],userid:["0001","0002"],
{unit:2,name: ["BA", "BB"],userid:["0011","0012"]};
if 'Unit' == 1 then display name array.
Basically I want loop through the object and retrieve the value.
I think you member should be like this
var members = [{unit:1,name: ["AA", "AB"],userid:["0001","0002"]},
{unit:2,name: ["BA", "BB"],userid:["0011","0012"]}];
var members = [{unit:1,name: ["AA", "AB"],userid:["0001","0002"]},
{unit:2,name: ["BA", "BB"],userid:["0011","0012"]}];
members.forEach(function(v,i){
if(v.unit==1)
{
console.log(v.name);
}
});
I guess this is want you wanted, an object within an object. Where 'unit' is the key for retrieving the objects within the object.
var members = {
1: {
name: ["test","test"],
userid: ["0001","0002"]},
2: {
name: ["test2", "test2"],
userid: ["0011", "0012"]}
};
console.log(members[1]);

Arrays inside objects [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to create an object that inside him have some fields, one of them supposed to be an array.
var ob=
{
name : "asdad"
array ?
}
But I dont know how to.
Can anyone help me?
Thank you!
a value is just a value, whether it be a simple scalar or a complicated looking object.
var obj = {
name: "user name",
stocks: ["AAPL", "GOOG"],
cars: [ {make:"Honda", model: "accord"},
{make:"BMW", model: "525", year: 2014}
]
};
do something like this:
var ob= {
name : "asdad",
myArray : ["value1", "value2", "etc."]
}
It would be helpful for you to go over this MDN doc.

Categories