javascript - How to concatenate JSON data into string? - Stack Overflow

I have {"Title":"Movie1","Year":"2013"} JSON data.I want to ge

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?

Share Improve this question edited May 3, 2021 at 9:54 T J 43.2k13 gold badges86 silver badges142 bronze badges asked Aug 1, 2014 at 10:36 user51854user51854 3191 gold badge4 silver badges11 bronze badges 4
  • 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
Add a ment  | 

5 Answers 5

Reset to default 3

If 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信