I have {"Title":"Movie1","Year":"2013"}
JSON data.
I want to get a string "Movie12013"
for that JSON. How to achieve that?
I have {"Title":"Movie1","Year":"2013"}
JSON data.
I want to get a string "Movie12013"
for that JSON. How to achieve that?
- It's my mistake that I didn't elaborate the question properly. I am running a loop where I will get a json data in which keys are not constant, so can't do all below given correct results. BTW those methods I know perfectly. I want to do it without using any key, value extraction. – user51854 Commented Aug 1, 2014 at 10:44
- 2 Rewrite your question then. Make sure you include what you've tried already. – Andy Commented Aug 1, 2014 at 10:46
- i don't know why vote down the answers if the question it is not enough to understand or to see clearly. – user468891 Commented Aug 1, 2014 at 10:55
- even though this question look like silly, but i think this website created to help each other humbly. whatever the questions was, whomever the asker was it doesn't matter, everyone deserve best answer. +1 vote up for this questions :) – Sal Prima Commented Apr 7, 2016 at 10:03
5 Answers
Reset to default 3If i understood correctly, you can do the following:
var json= {"Title":"Movie1","Year":"2013"};
var result="";
for( key in json){
result+= json[key];
}
You don't have to know the number of properties or it's names before hand. This should work for simple scenarios.
Demo try this,
var json= {"Title":"Movie1","Year":"2013"};
var append="";
$.each(json,function(key,value){
append+=value;
});
You first need to extract the JSON data using JSON.parse()
:
var data = JSON.parse(json);
This assumes your JSON data is held in a variable named json
which we've passed into the parse()
method. This gives us the following JavaScript object:
{
Title: "Movie1",
Year: "2013"
}
We can now join the two values simply by concatenating them with the +
symbol:
var result = data.Title + data.Year; // "Movie12013"
try something like this
var jsonText = '{"Title":"Movie1","Year":"2013"}'
obj = JSON.parse(jsonText);
var string = obj.Title + obj.Year;
Assuming the JSON data is kept in a jsonVarName
variable:
jsonVarName.title + jsonVarName.year
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744906568a4600293.html
评论列表(0条)