0

I am unable to create a Json string in the following format, please help. Where key value pair can be a number:

"tag_container": { "tag1": "Tag1", "tag2": "Tag2", ..... }   
var uploaded = new Uploaded();
var str = "*#Hello* i *#am*  writing a regexp *#h*";
var re = hash_parser(str);  
uploaded.tag_list = new Array;
uploaded.tag_list.tag = new Array;

for(var i = 0; i < re.length; i++)
{
    uploaded.tag_list[i] = new Object;
    uploaded.tag_list[i].**tag** = re[i];
}

above code is giving in following format:

"tag_list":[{"**tag**":"*#Hello*"},{"**tag**":"*#am*"},{"**tag**":"*#h*"}]
4
  • 1
    Sorry, can you explain a bit more? First of all, the code does not create a string at all. Second, what exactly is the problem with the output format? Commented Dec 7, 2010 at 5:03
  • Your code does not make sense. Arrays are created like this: uploaded.tag_list = [] or like this: uploaded.tag_list = new Array(). Likewise, objects are created like {} or new Object(). You also seem confused about JSON. Did you want to wrap your JSON examples in single quotes? Like this: '"tag_container": { "tag1": "Tag1", "tag2": "Tag2", ..... }' Commented Dec 7, 2010 at 5:03
  • thanks for the quick reply i am able to generate in "tag_list":["#Hello","#am","#h"] in javascript in following formate but i need in this formate "tag_list": { "tag1": "#Hello", "tag2": "#am", ..... } Commented Dec 7, 2010 at 5:26
  • I think he's trying to say that he wants to have a JSON "associative array", which is accomplished in Javascript by using objects. As I say in my answer, this is done by setting tag_list to an object, and using this notation to enter key=>value pairs: uploaded.tag_list['tag' + (i+1)] = re[i]; Commented Dec 7, 2010 at 6:53

4 Answers 4

3

I think maybe you might be confusing JSON with object literal syntax.

Instead of:

"tag_container": { "tag1": "Tag1", "tag2": "Tag2", ..... }   

you should just be using normal JS syntax:

var tag_container = { "tag1": "Tag1", "tag2": "Tag2", ..... }

... but it's really hard to tell from your post. If you know anyone who can help you with your English, it really might help me (and others) understand this question better.

0

The easiest way to convert something into JSON is to use JSON.stringify.

var jsonString = JSON.stringify({ tag_container: { tag1: 'tag1', tag2: 'tag2' }});

stringify is available in all modern browsers. If you wish to support older versions, the JSON2 library is perhaps the best choice as it provides the same API as the official JSON spec.

0

I think what you're trying to do is something like this:

var reLen = re.length;
uploaded.tag_list = {};

for(var i = 0; i < reLen; i++)
{
    uploaded.tag_list['tag' + (i+1)] = re[i];
}

This will output the format you wish to see, i.e.

'"tag_container": {"tag1": "*#Hello*", "tag2": "*#am*", "tag3": "*#h*"}'
-1

Thank you all for the all reply and guide my problem is solved in some other way I am sending json string to cross domain

string = "hi  @group1 @group2  *user1 *user2 #fs #ffsd #fsdf";

and generated json string is

{"raw_msg":"hi @group1 @group2 *user1 *user2 #fs #ffsd #fsdf","msg_type":"group","top_msg_id":0,"file_container":{"file1":{"id":"","file_name":"","uri":""},"file2":{"id":"","file_name":"","uri":""}},***"tag_list":["fs","ffsd","fsdf"]***,"link_list":"","image_container":{"image1":{"id":"","name":"Name","uri":""},"image2":{"id":"","name":"Name","uri":""}},"attached_thread":{"thread1":{"title":"","top_id":"","last_id":""},"thread2":{"title":"","top_id":"","last_id":""}},"to_user":["user1","user2"],"to_group":["group1","group2"]}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.