I want to add a JSONObject to a JSONArray and that JSONArray included in other JSONObject
I need below kind of structure constructed in java and send it as response :
var abc = {"action":"Remove",
"datatable":[
{"userid":"userid0","username":"name0"},
{"userid":"userid1","username":"name1"},
{"userid":"userid2","username":"name2"},
{"userid":"userid3","username":"name3"}
]
,
"msgType":"success"};
I am doing:
JSONArray jsonArray = new JSONArray();
for (loop) {
JSONObject jsonObj= new JSONObject();
jsonObj.put("srcOfPhoto", srcOfPhoto);
jsonObj.put("username", "name"+count);
jsonObj.put("userid", "userid"+count);
jsonArray .add(jsonObj.toJSONString());
}
Map paramMap = new HashMap();
paramMap.put("action", "remove");
paramMap.put("datatable", jsonArray );
paramMap.put(Constant.MSG_TYPE , Constant.SUCCESS);
getJSONMessage(paramMap);
and herer above function is converting paramMap into json string like:
public static String getJSONMessage(Map<String, String> paramMap){
if(paramMap != null && paramMap.size() > 0)
return JSONObject.toJSONString(paramMap);
else
return "";
}
but it is not creating the right structure, can anybody help me in this?
here is what I am getting output:
{"action":"Remove","datatable":["{"userid":"userid0","srcOfPhoto":"users/JMMBGTCHG.jpg","username":"name0"}"],"msgType":"success"}
which is not being parsed in javascript.
var json = eval('(' + respText+')');
alert("contents>>"+json.datatable);
alert("contents.datatable[0]>>>"+json.datatable[0].username);
last alert showing undefined.
ohh sorry I forgot to paste last line , here is the last line:
getJSONMessage(paramMap);
and above function is converting paramMap into json string:
public static String getJSONMessage(Map<String, String> paramMap){
if(paramMap != null && paramMap.size() > 0)
return JSONObject.toJSONString(paramMap);
else
return "";
}
json arrays
add a comment |
I need below kind of structure constructed in java and send it as response :
var abc = {"action":"Remove",
"datatable":[
{"userid":"userid0","username":"name0"},
{"userid":"userid1","username":"name1"},
{"userid":"userid2","username":"name2"},
{"userid":"userid3","username":"name3"}
]
,
"msgType":"success"};
I am doing:
JSONArray jsonArray = new JSONArray();
for (loop) {
JSONObject jsonObj= new JSONObject();
jsonObj.put("srcOfPhoto", srcOfPhoto);
jsonObj.put("username", "name"+count);
jsonObj.put("userid", "userid"+count);
jsonArray .add(jsonObj.toJSONString());
}
Map paramMap = new HashMap();
paramMap.put("action", "remove");
paramMap.put("datatable", jsonArray );
paramMap.put(Constant.MSG_TYPE , Constant.SUCCESS);
getJSONMessage(paramMap);
and herer above function is converting paramMap into json string like:
public static String getJSONMessage(Map<String, String> paramMap){
if(paramMap != null && paramMap.size() > 0)
return JSONObject.toJSONString(paramMap);
else
return "";
}
but it is not creating the right structure, can anybody help me in this?
here is what I am getting output:
{"action":"Remove","datatable":["{"userid":"userid0","srcOfPhoto":"users/JMMBGTCHG.jpg","username":"name0"}"],"msgType":"success"}
which is not being parsed in javascript.
var json = eval('(' + respText+')');
alert("contents>>"+json.datatable);
alert("contents.datatable[0]>>>"+json.datatable[0].username);
last alert showing undefined.
ohh sorry I forgot to paste last line , here is the last line:
getJSONMessage(paramMap);
and above function is converting paramMap into json string:
public static String getJSONMessage(Map<String, String> paramMap){
if(paramMap != null && paramMap.size() > 0)
return JSONObject.toJSONString(paramMap);
else
return "";
}
json arrays
I've merged your two accounts together. Please read this Faq entry about cookie-based accounts. Also, StackOverflow isn't a forum; if you have a new question, please ask a new question. If you want to include more information in your question, please edit it. If you want to interact with one of the people who has answered, you can leave them a comment.
– Will
Sep 21 '11 at 12:12
add a comment |
I need below kind of structure constructed in java and send it as response :
var abc = {"action":"Remove",
"datatable":[
{"userid":"userid0","username":"name0"},
{"userid":"userid1","username":"name1"},
{"userid":"userid2","username":"name2"},
{"userid":"userid3","username":"name3"}
]
,
"msgType":"success"};
I am doing:
JSONArray jsonArray = new JSONArray();
for (loop) {
JSONObject jsonObj= new JSONObject();
jsonObj.put("srcOfPhoto", srcOfPhoto);
jsonObj.put("username", "name"+count);
jsonObj.put("userid", "userid"+count);
jsonArray .add(jsonObj.toJSONString());
}
Map paramMap = new HashMap();
paramMap.put("action", "remove");
paramMap.put("datatable", jsonArray );
paramMap.put(Constant.MSG_TYPE , Constant.SUCCESS);
getJSONMessage(paramMap);
and herer above function is converting paramMap into json string like:
public static String getJSONMessage(Map<String, String> paramMap){
if(paramMap != null && paramMap.size() > 0)
return JSONObject.toJSONString(paramMap);
else
return "";
}
but it is not creating the right structure, can anybody help me in this?
here is what I am getting output:
{"action":"Remove","datatable":["{"userid":"userid0","srcOfPhoto":"users/JMMBGTCHG.jpg","username":"name0"}"],"msgType":"success"}
which is not being parsed in javascript.
var json = eval('(' + respText+')');
alert("contents>>"+json.datatable);
alert("contents.datatable[0]>>>"+json.datatable[0].username);
last alert showing undefined.
ohh sorry I forgot to paste last line , here is the last line:
getJSONMessage(paramMap);
and above function is converting paramMap into json string:
public static String getJSONMessage(Map<String, String> paramMap){
if(paramMap != null && paramMap.size() > 0)
return JSONObject.toJSONString(paramMap);
else
return "";
}
json arrays
I need below kind of structure constructed in java and send it as response :
var abc = {"action":"Remove",
"datatable":[
{"userid":"userid0","username":"name0"},
{"userid":"userid1","username":"name1"},
{"userid":"userid2","username":"name2"},
{"userid":"userid3","username":"name3"}
]
,
"msgType":"success"};
I am doing:
JSONArray jsonArray = new JSONArray();
for (loop) {
JSONObject jsonObj= new JSONObject();
jsonObj.put("srcOfPhoto", srcOfPhoto);
jsonObj.put("username", "name"+count);
jsonObj.put("userid", "userid"+count);
jsonArray .add(jsonObj.toJSONString());
}
Map paramMap = new HashMap();
paramMap.put("action", "remove");
paramMap.put("datatable", jsonArray );
paramMap.put(Constant.MSG_TYPE , Constant.SUCCESS);
getJSONMessage(paramMap);
and herer above function is converting paramMap into json string like:
public static String getJSONMessage(Map<String, String> paramMap){
if(paramMap != null && paramMap.size() > 0)
return JSONObject.toJSONString(paramMap);
else
return "";
}
but it is not creating the right structure, can anybody help me in this?
here is what I am getting output:
{"action":"Remove","datatable":["{"userid":"userid0","srcOfPhoto":"users/JMMBGTCHG.jpg","username":"name0"}"],"msgType":"success"}
which is not being parsed in javascript.
var json = eval('(' + respText+')');
alert("contents>>"+json.datatable);
alert("contents.datatable[0]>>>"+json.datatable[0].username);
last alert showing undefined.
ohh sorry I forgot to paste last line , here is the last line:
getJSONMessage(paramMap);
and above function is converting paramMap into json string:
public static String getJSONMessage(Map<String, String> paramMap){
if(paramMap != null && paramMap.size() > 0)
return JSONObject.toJSONString(paramMap);
else
return "";
}
json arrays
json arrays
edited Jul 6 '17 at 7:46
Cassio Mazzochi Molin
56.6k17104174
56.6k17104174
asked Sep 21 '11 at 10:02
AjkaAjka
48113
48113
I've merged your two accounts together. Please read this Faq entry about cookie-based accounts. Also, StackOverflow isn't a forum; if you have a new question, please ask a new question. If you want to include more information in your question, please edit it. If you want to interact with one of the people who has answered, you can leave them a comment.
– Will
Sep 21 '11 at 12:12
add a comment |
I've merged your two accounts together. Please read this Faq entry about cookie-based accounts. Also, StackOverflow isn't a forum; if you have a new question, please ask a new question. If you want to include more information in your question, please edit it. If you want to interact with one of the people who has answered, you can leave them a comment.
– Will
Sep 21 '11 at 12:12
I've merged your two accounts together. Please read this Faq entry about cookie-based accounts. Also, StackOverflow isn't a forum; if you have a new question, please ask a new question. If you want to include more information in your question, please edit it. If you want to interact with one of the people who has answered, you can leave them a comment.
– Will
Sep 21 '11 at 12:12
I've merged your two accounts together. Please read this Faq entry about cookie-based accounts. Also, StackOverflow isn't a forum; if you have a new question, please ask a new question. If you want to include more information in your question, please edit it. If you want to interact with one of the people who has answered, you can leave them a comment.
– Will
Sep 21 '11 at 12:12
add a comment |
2 Answers
2
active
oldest
votes
JSONArray jsonArray = new JSONArray();
for (loop) {
JSONObject jsonObj= new JSONObject();
jsonObj.put("srcOfPhoto", srcOfPhoto);
jsonObj.put("username", "name"+count);
jsonObj.put("userid", "userid"+count);
jsonArray.put(jsonObj.valueToString());
}
JSONObject parameters = new JSONObject();
parameters.put("action", "remove");
parameters.put("datatable", jsonArray );
parameters.put(Constant.MSG_TYPE , Constant.SUCCESS);
Why were you using an Hashmap if what you wanted was to put it into a JSONObject?
EDIT: As per http://www.json.org/javadoc/org/json/JSONArray.html
EDIT2: On the JSONObject method used, I'm following the code available at: https://github.com/stleary/JSON-java/blob/master/JSONObject.java#L2327 , that method is not deprecated.
We're storing a string representation of the JSONObject, not the JSONObject itself
3
there's noadd(jsonObj)
, butput(jsonObj)
is possible (it doesn't seem necesary to convert to JSONString either)
– Someone Somewhere
Feb 24 '12 at 5:51
@SomeoneSomewhere:There is add method:
– Shahzad Imam
Apr 16 '12 at 12:59
The method toJSONString() is undefined for the type JSONObject ??
– Basbous
Mar 25 '13 at 8:48
it appears that the standard changed tovalueToString()
will edit.
– Gonçalo Vieira
Mar 25 '13 at 12:32
There is no push() method in jsonArray object.
– Zin Win Htet
Jun 10 '14 at 9:33
|
show 1 more comment
JSONArray successObject=new JSONArray();
JSONObject dataObject=new JSONObject();
successObject.put(dataObject.toString());
This works for me.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7497980%2fi-want-to-add-a-jsonobject-to-a-jsonarray-and-that-jsonarray-included-in-other-j%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
JSONArray jsonArray = new JSONArray();
for (loop) {
JSONObject jsonObj= new JSONObject();
jsonObj.put("srcOfPhoto", srcOfPhoto);
jsonObj.put("username", "name"+count);
jsonObj.put("userid", "userid"+count);
jsonArray.put(jsonObj.valueToString());
}
JSONObject parameters = new JSONObject();
parameters.put("action", "remove");
parameters.put("datatable", jsonArray );
parameters.put(Constant.MSG_TYPE , Constant.SUCCESS);
Why were you using an Hashmap if what you wanted was to put it into a JSONObject?
EDIT: As per http://www.json.org/javadoc/org/json/JSONArray.html
EDIT2: On the JSONObject method used, I'm following the code available at: https://github.com/stleary/JSON-java/blob/master/JSONObject.java#L2327 , that method is not deprecated.
We're storing a string representation of the JSONObject, not the JSONObject itself
3
there's noadd(jsonObj)
, butput(jsonObj)
is possible (it doesn't seem necesary to convert to JSONString either)
– Someone Somewhere
Feb 24 '12 at 5:51
@SomeoneSomewhere:There is add method:
– Shahzad Imam
Apr 16 '12 at 12:59
The method toJSONString() is undefined for the type JSONObject ??
– Basbous
Mar 25 '13 at 8:48
it appears that the standard changed tovalueToString()
will edit.
– Gonçalo Vieira
Mar 25 '13 at 12:32
There is no push() method in jsonArray object.
– Zin Win Htet
Jun 10 '14 at 9:33
|
show 1 more comment
JSONArray jsonArray = new JSONArray();
for (loop) {
JSONObject jsonObj= new JSONObject();
jsonObj.put("srcOfPhoto", srcOfPhoto);
jsonObj.put("username", "name"+count);
jsonObj.put("userid", "userid"+count);
jsonArray.put(jsonObj.valueToString());
}
JSONObject parameters = new JSONObject();
parameters.put("action", "remove");
parameters.put("datatable", jsonArray );
parameters.put(Constant.MSG_TYPE , Constant.SUCCESS);
Why were you using an Hashmap if what you wanted was to put it into a JSONObject?
EDIT: As per http://www.json.org/javadoc/org/json/JSONArray.html
EDIT2: On the JSONObject method used, I'm following the code available at: https://github.com/stleary/JSON-java/blob/master/JSONObject.java#L2327 , that method is not deprecated.
We're storing a string representation of the JSONObject, not the JSONObject itself
3
there's noadd(jsonObj)
, butput(jsonObj)
is possible (it doesn't seem necesary to convert to JSONString either)
– Someone Somewhere
Feb 24 '12 at 5:51
@SomeoneSomewhere:There is add method:
– Shahzad Imam
Apr 16 '12 at 12:59
The method toJSONString() is undefined for the type JSONObject ??
– Basbous
Mar 25 '13 at 8:48
it appears that the standard changed tovalueToString()
will edit.
– Gonçalo Vieira
Mar 25 '13 at 12:32
There is no push() method in jsonArray object.
– Zin Win Htet
Jun 10 '14 at 9:33
|
show 1 more comment
JSONArray jsonArray = new JSONArray();
for (loop) {
JSONObject jsonObj= new JSONObject();
jsonObj.put("srcOfPhoto", srcOfPhoto);
jsonObj.put("username", "name"+count);
jsonObj.put("userid", "userid"+count);
jsonArray.put(jsonObj.valueToString());
}
JSONObject parameters = new JSONObject();
parameters.put("action", "remove");
parameters.put("datatable", jsonArray );
parameters.put(Constant.MSG_TYPE , Constant.SUCCESS);
Why were you using an Hashmap if what you wanted was to put it into a JSONObject?
EDIT: As per http://www.json.org/javadoc/org/json/JSONArray.html
EDIT2: On the JSONObject method used, I'm following the code available at: https://github.com/stleary/JSON-java/blob/master/JSONObject.java#L2327 , that method is not deprecated.
We're storing a string representation of the JSONObject, not the JSONObject itself
JSONArray jsonArray = new JSONArray();
for (loop) {
JSONObject jsonObj= new JSONObject();
jsonObj.put("srcOfPhoto", srcOfPhoto);
jsonObj.put("username", "name"+count);
jsonObj.put("userid", "userid"+count);
jsonArray.put(jsonObj.valueToString());
}
JSONObject parameters = new JSONObject();
parameters.put("action", "remove");
parameters.put("datatable", jsonArray );
parameters.put(Constant.MSG_TYPE , Constant.SUCCESS);
Why were you using an Hashmap if what you wanted was to put it into a JSONObject?
EDIT: As per http://www.json.org/javadoc/org/json/JSONArray.html
EDIT2: On the JSONObject method used, I'm following the code available at: https://github.com/stleary/JSON-java/blob/master/JSONObject.java#L2327 , that method is not deprecated.
We're storing a string representation of the JSONObject, not the JSONObject itself
edited Aug 29 '18 at 16:02
answered Sep 21 '11 at 10:10
Gonçalo VieiraGonçalo Vieira
1,18211134
1,18211134
3
there's noadd(jsonObj)
, butput(jsonObj)
is possible (it doesn't seem necesary to convert to JSONString either)
– Someone Somewhere
Feb 24 '12 at 5:51
@SomeoneSomewhere:There is add method:
– Shahzad Imam
Apr 16 '12 at 12:59
The method toJSONString() is undefined for the type JSONObject ??
– Basbous
Mar 25 '13 at 8:48
it appears that the standard changed tovalueToString()
will edit.
– Gonçalo Vieira
Mar 25 '13 at 12:32
There is no push() method in jsonArray object.
– Zin Win Htet
Jun 10 '14 at 9:33
|
show 1 more comment
3
there's noadd(jsonObj)
, butput(jsonObj)
is possible (it doesn't seem necesary to convert to JSONString either)
– Someone Somewhere
Feb 24 '12 at 5:51
@SomeoneSomewhere:There is add method:
– Shahzad Imam
Apr 16 '12 at 12:59
The method toJSONString() is undefined for the type JSONObject ??
– Basbous
Mar 25 '13 at 8:48
it appears that the standard changed tovalueToString()
will edit.
– Gonçalo Vieira
Mar 25 '13 at 12:32
There is no push() method in jsonArray object.
– Zin Win Htet
Jun 10 '14 at 9:33
3
3
there's no
add(jsonObj)
, but put(jsonObj)
is possible (it doesn't seem necesary to convert to JSONString either)– Someone Somewhere
Feb 24 '12 at 5:51
there's no
add(jsonObj)
, but put(jsonObj)
is possible (it doesn't seem necesary to convert to JSONString either)– Someone Somewhere
Feb 24 '12 at 5:51
@SomeoneSomewhere:There is add method:
– Shahzad Imam
Apr 16 '12 at 12:59
@SomeoneSomewhere:There is add method:
– Shahzad Imam
Apr 16 '12 at 12:59
The method toJSONString() is undefined for the type JSONObject ??
– Basbous
Mar 25 '13 at 8:48
The method toJSONString() is undefined for the type JSONObject ??
– Basbous
Mar 25 '13 at 8:48
it appears that the standard changed to
valueToString()
will edit.– Gonçalo Vieira
Mar 25 '13 at 12:32
it appears that the standard changed to
valueToString()
will edit.– Gonçalo Vieira
Mar 25 '13 at 12:32
There is no push() method in jsonArray object.
– Zin Win Htet
Jun 10 '14 at 9:33
There is no push() method in jsonArray object.
– Zin Win Htet
Jun 10 '14 at 9:33
|
show 1 more comment
JSONArray successObject=new JSONArray();
JSONObject dataObject=new JSONObject();
successObject.put(dataObject.toString());
This works for me.
add a comment |
JSONArray successObject=new JSONArray();
JSONObject dataObject=new JSONObject();
successObject.put(dataObject.toString());
This works for me.
add a comment |
JSONArray successObject=new JSONArray();
JSONObject dataObject=new JSONObject();
successObject.put(dataObject.toString());
This works for me.
JSONArray successObject=new JSONArray();
JSONObject dataObject=new JSONObject();
successObject.put(dataObject.toString());
This works for me.
edited Nov 22 '18 at 8:16
Unheilig
12k165383
12k165383
answered Nov 22 '18 at 7:56
sagar varadesagar varade
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7497980%2fi-want-to-add-a-jsonobject-to-a-jsonarray-and-that-jsonarray-included-in-other-j%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I've merged your two accounts together. Please read this Faq entry about cookie-based accounts. Also, StackOverflow isn't a forum; if you have a new question, please ask a new question. If you want to include more information in your question, please edit it. If you want to interact with one of the people who has answered, you can leave them a comment.
– Will
Sep 21 '11 at 12:12