반응형
Javascript 개체를 Json 문자열로 인코딩하는 중
Javascript 오브젝트를 JSON 문자열로 인코딩하고 싶은데 큰 어려움을 겪고 있습니다.
개체는 다음과 같습니다.
new_tweets[k]['tweet_id'] = 98745521;
new_tweets[k]['user_id'] = 54875;
new_tweets[k]['data']['in_reply_to_screen_name'] = "other_user";
new_tweets[k]['data']['text'] = "tweet text";
이것을 JSON 문자열에 넣어 Ajax 요구에 넣고 싶다.
{'k':{'tweet_id':98745521,'user_id':54875, 'data':{...}}}
상황을 이해하게 될 거야내가 뭘 해도 소용없어.json2와 같은 모든 JSON 인코더 및 제품
[]
글쎄, 그건 도움이 안 돼요기본적으로는 php 같은 것을 갖고 싶습니다.encodejson
기능.
변수가 없는 한k
정의되어 있기 때문에 문제가 생겼을 겁니다.다음과 같은 방법으로 원하는 작업을 수행할 수 있습니다.
var new_tweets = { };
new_tweets.k = { };
new_tweets.k.tweet_id = 98745521;
new_tweets.k.user_id = 54875;
new_tweets.k.data = { };
new_tweets.k.data.in_reply_to_screen_name = 'other_user';
new_tweets.k.data.text = 'tweet text';
// Will create the JSON string you're looking for.
var json = JSON.stringify(new_tweets);
한 번에 모든 작업을 수행할 수도 있습니다.
var new_tweets = {
k: {
tweet_id: 98745521,
user_id: 54875,
data: {
in_reply_to_screen_name: 'other_user',
text: 'tweet_text'
}
}
}
다음과 같이 사용할 수 있습니다.
JSON.stringify(new_tweets);
언급URL : https://stackoverflow.com/questions/6810084/encoding-javascript-object-to-json-string
반응형
'programing' 카테고리의 다른 글
현재 폴더에 직접 React 앱을 만드는 방법 (0) | 2023.03.04 |
---|---|
파일을 번들하지 않도록 웹 팩 가져오기 (0) | 2023.03.04 |
JDeveloper/SQL Developer가 자격 증명을 유지하기 위해 사용하는 암호화 기술을 아는 사람이 있습니까? (0) | 2023.03.04 |
여러 개의 대소문자가 있는 Tymleaf 스위치 문 (0) | 2023.03.04 |
Mongo 그룹 및 푸시: 모든 필드 푸시 (0) | 2023.03.04 |