How to retrieve data from json object in java?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to retrieve Value i.e., 80000
where Name is "Camera" from the Product's JSON using java. Can anyone please help me out ?
{
"Products":{
"Product":[
{
"Name":"Tv",
"Value":50000
},
{
"Name":"Camera",
"Value":80000
},
{
"Name":"Phone",
"Value":15000
},
]
}
}
Mycode:
JSONObject arrayOfProducts = jsonObj.optJSONObject("Products");
JSONArray products = arrayOfProducts.getJSONArray("Product");
for (int i = 0; i < products.length(); i++) {
JSONObject objects = products.getJSONObject(i);
Iterator key = objects.keys();
while (key.hasNext()) {
String k = key.next().toString();
if(k.equals("Name")) {
if(objects.getString(k).equals("Camera")) {
System.out.println("Key : " + k + ", value : " + objects.getString(k));
}
}
}
java json
add a comment |
I'm trying to retrieve Value i.e., 80000
where Name is "Camera" from the Product's JSON using java. Can anyone please help me out ?
{
"Products":{
"Product":[
{
"Name":"Tv",
"Value":50000
},
{
"Name":"Camera",
"Value":80000
},
{
"Name":"Phone",
"Value":15000
},
]
}
}
Mycode:
JSONObject arrayOfProducts = jsonObj.optJSONObject("Products");
JSONArray products = arrayOfProducts.getJSONArray("Product");
for (int i = 0; i < products.length(); i++) {
JSONObject objects = products.getJSONObject(i);
Iterator key = objects.keys();
while (key.hasNext()) {
String k = key.next().toString();
if(k.equals("Name")) {
if(objects.getString(k).equals("Camera")) {
System.out.println("Key : " + k + ", value : " + objects.getString(k));
}
}
}
java json
1
Possible duplicate of How to parse JSON in Java
– Dang Nguyen
Nov 29 '18 at 6:03
Your code look normal. But your JSON is not good. Strange quotes and JSON object must begin with "{" and end with "}". How do you create jsonObj? What error do you see? P.S. Your can take "Name" like this: objects.getString("Name") and check "Name" existing (if you need) like this: objects.has("Name")
– Anatoly Samoylenko
Nov 29 '18 at 6:20
This is a part of json. jsonObj contains entire json through which I'm trying to parse the json. Edited quotes
– fervent
Nov 29 '18 at 6:24
Can you please edit the above code to retrieve the value of Camera i.e., 80000 ?
– fervent
Nov 29 '18 at 6:25
replace System.out.println("Key : " + k + ", value : " + objects.getString(k)); on System.out.println("Key : " + k + ", name : " + objects.getString(k) + ", value : " + objects.getInt("Value"));
– Anatoly Samoylenko
Nov 29 '18 at 6:31
add a comment |
I'm trying to retrieve Value i.e., 80000
where Name is "Camera" from the Product's JSON using java. Can anyone please help me out ?
{
"Products":{
"Product":[
{
"Name":"Tv",
"Value":50000
},
{
"Name":"Camera",
"Value":80000
},
{
"Name":"Phone",
"Value":15000
},
]
}
}
Mycode:
JSONObject arrayOfProducts = jsonObj.optJSONObject("Products");
JSONArray products = arrayOfProducts.getJSONArray("Product");
for (int i = 0; i < products.length(); i++) {
JSONObject objects = products.getJSONObject(i);
Iterator key = objects.keys();
while (key.hasNext()) {
String k = key.next().toString();
if(k.equals("Name")) {
if(objects.getString(k).equals("Camera")) {
System.out.println("Key : " + k + ", value : " + objects.getString(k));
}
}
}
java json
I'm trying to retrieve Value i.e., 80000
where Name is "Camera" from the Product's JSON using java. Can anyone please help me out ?
{
"Products":{
"Product":[
{
"Name":"Tv",
"Value":50000
},
{
"Name":"Camera",
"Value":80000
},
{
"Name":"Phone",
"Value":15000
},
]
}
}
Mycode:
JSONObject arrayOfProducts = jsonObj.optJSONObject("Products");
JSONArray products = arrayOfProducts.getJSONArray("Product");
for (int i = 0; i < products.length(); i++) {
JSONObject objects = products.getJSONObject(i);
Iterator key = objects.keys();
while (key.hasNext()) {
String k = key.next().toString();
if(k.equals("Name")) {
if(objects.getString(k).equals("Camera")) {
System.out.println("Key : " + k + ", value : " + objects.getString(k));
}
}
}
java json
java json
edited Nov 29 '18 at 9:26
Rai
1,1444823
1,1444823
asked Nov 29 '18 at 5:59
ferventfervent
287
287
1
Possible duplicate of How to parse JSON in Java
– Dang Nguyen
Nov 29 '18 at 6:03
Your code look normal. But your JSON is not good. Strange quotes and JSON object must begin with "{" and end with "}". How do you create jsonObj? What error do you see? P.S. Your can take "Name" like this: objects.getString("Name") and check "Name" existing (if you need) like this: objects.has("Name")
– Anatoly Samoylenko
Nov 29 '18 at 6:20
This is a part of json. jsonObj contains entire json through which I'm trying to parse the json. Edited quotes
– fervent
Nov 29 '18 at 6:24
Can you please edit the above code to retrieve the value of Camera i.e., 80000 ?
– fervent
Nov 29 '18 at 6:25
replace System.out.println("Key : " + k + ", value : " + objects.getString(k)); on System.out.println("Key : " + k + ", name : " + objects.getString(k) + ", value : " + objects.getInt("Value"));
– Anatoly Samoylenko
Nov 29 '18 at 6:31
add a comment |
1
Possible duplicate of How to parse JSON in Java
– Dang Nguyen
Nov 29 '18 at 6:03
Your code look normal. But your JSON is not good. Strange quotes and JSON object must begin with "{" and end with "}". How do you create jsonObj? What error do you see? P.S. Your can take "Name" like this: objects.getString("Name") and check "Name" existing (if you need) like this: objects.has("Name")
– Anatoly Samoylenko
Nov 29 '18 at 6:20
This is a part of json. jsonObj contains entire json through which I'm trying to parse the json. Edited quotes
– fervent
Nov 29 '18 at 6:24
Can you please edit the above code to retrieve the value of Camera i.e., 80000 ?
– fervent
Nov 29 '18 at 6:25
replace System.out.println("Key : " + k + ", value : " + objects.getString(k)); on System.out.println("Key : " + k + ", name : " + objects.getString(k) + ", value : " + objects.getInt("Value"));
– Anatoly Samoylenko
Nov 29 '18 at 6:31
1
1
Possible duplicate of How to parse JSON in Java
– Dang Nguyen
Nov 29 '18 at 6:03
Possible duplicate of How to parse JSON in Java
– Dang Nguyen
Nov 29 '18 at 6:03
Your code look normal. But your JSON is not good. Strange quotes and JSON object must begin with "{" and end with "}". How do you create jsonObj? What error do you see? P.S. Your can take "Name" like this: objects.getString("Name") and check "Name" existing (if you need) like this: objects.has("Name")
– Anatoly Samoylenko
Nov 29 '18 at 6:20
Your code look normal. But your JSON is not good. Strange quotes and JSON object must begin with "{" and end with "}". How do you create jsonObj? What error do you see? P.S. Your can take "Name" like this: objects.getString("Name") and check "Name" existing (if you need) like this: objects.has("Name")
– Anatoly Samoylenko
Nov 29 '18 at 6:20
This is a part of json. jsonObj contains entire json through which I'm trying to parse the json. Edited quotes
– fervent
Nov 29 '18 at 6:24
This is a part of json. jsonObj contains entire json through which I'm trying to parse the json. Edited quotes
– fervent
Nov 29 '18 at 6:24
Can you please edit the above code to retrieve the value of Camera i.e., 80000 ?
– fervent
Nov 29 '18 at 6:25
Can you please edit the above code to retrieve the value of Camera i.e., 80000 ?
– fervent
Nov 29 '18 at 6:25
replace System.out.println("Key : " + k + ", value : " + objects.getString(k)); on System.out.println("Key : " + k + ", name : " + objects.getString(k) + ", value : " + objects.getInt("Value"));
– Anatoly Samoylenko
Nov 29 '18 at 6:31
replace System.out.println("Key : " + k + ", value : " + objects.getString(k)); on System.out.println("Key : " + k + ", name : " + objects.getString(k) + ", value : " + objects.getInt("Value"));
– Anatoly Samoylenko
Nov 29 '18 at 6:31
add a comment |
3 Answers
3
active
oldest
votes
If you want to get the value in a key-value pair in a JSON you need to use the following syntax:
[JSONObject].get[data_type]([key_name])
.
In the above given syntax, replace the
[JSONObject]
with the
variable of typeJSONObject
representing the JSON. In your case,
it isobjects
.
replace
[data_type]
with the data type of the value at that particular key. In the
current case, value for the key"Value"
is80000
is anInteger
.. Hence it
should begetInt
.
Replace
[key_name]
with the key whose value you need to retrieve, which in your
case is"Value"
.
Hence the code snippet you need to use to get the value 80000 which with the "Camera" part is: objects.getInt("Value")
.
Here is your overall updated code:
JSONObject arrayOfProducts = jsonObj.optJSONObject("Products");
JSONArray products = arrayOfProducts.getJSONArray("Product");
for (int i = 0; i < products.length(); i++) {
JSONObject objects = products.getJSONObject(i);
Iterator key = objects.keys();
while (key.hasNext()) {
String k = key.next().toString();
if(k.equals("Name")) {
if(objects.getString(k).equals("Camera")) {
System.out.println("nKey : " + k + "nName : " + objects.getString(k) + ", nValue : " + objects.getInt("Value"));
}
}
}
}
add a comment |
Your JSON is wrong.
- Don't use left/right quotes. Instead use quotes without a direction.
Correct: "Products"
Wrong: “Products”
- Each JSON object should be enclosed in
{}
. Enclose your whole JSON file content within{}
.
add a comment |
Why you need to choose complex solution to parse JSON like that?
You should use define some entities and then use library for reading JSON value (Ex: "ObjectMapper" of jackson) to parser it to Object.
You can leave all complex parts for them, after that you just working on java object.
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%2f53532730%2fhow-to-retrieve-data-from-json-object-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to get the value in a key-value pair in a JSON you need to use the following syntax:
[JSONObject].get[data_type]([key_name])
.
In the above given syntax, replace the
[JSONObject]
with the
variable of typeJSONObject
representing the JSON. In your case,
it isobjects
.
replace
[data_type]
with the data type of the value at that particular key. In the
current case, value for the key"Value"
is80000
is anInteger
.. Hence it
should begetInt
.
Replace
[key_name]
with the key whose value you need to retrieve, which in your
case is"Value"
.
Hence the code snippet you need to use to get the value 80000 which with the "Camera" part is: objects.getInt("Value")
.
Here is your overall updated code:
JSONObject arrayOfProducts = jsonObj.optJSONObject("Products");
JSONArray products = arrayOfProducts.getJSONArray("Product");
for (int i = 0; i < products.length(); i++) {
JSONObject objects = products.getJSONObject(i);
Iterator key = objects.keys();
while (key.hasNext()) {
String k = key.next().toString();
if(k.equals("Name")) {
if(objects.getString(k).equals("Camera")) {
System.out.println("nKey : " + k + "nName : " + objects.getString(k) + ", nValue : " + objects.getInt("Value"));
}
}
}
}
add a comment |
If you want to get the value in a key-value pair in a JSON you need to use the following syntax:
[JSONObject].get[data_type]([key_name])
.
In the above given syntax, replace the
[JSONObject]
with the
variable of typeJSONObject
representing the JSON. In your case,
it isobjects
.
replace
[data_type]
with the data type of the value at that particular key. In the
current case, value for the key"Value"
is80000
is anInteger
.. Hence it
should begetInt
.
Replace
[key_name]
with the key whose value you need to retrieve, which in your
case is"Value"
.
Hence the code snippet you need to use to get the value 80000 which with the "Camera" part is: objects.getInt("Value")
.
Here is your overall updated code:
JSONObject arrayOfProducts = jsonObj.optJSONObject("Products");
JSONArray products = arrayOfProducts.getJSONArray("Product");
for (int i = 0; i < products.length(); i++) {
JSONObject objects = products.getJSONObject(i);
Iterator key = objects.keys();
while (key.hasNext()) {
String k = key.next().toString();
if(k.equals("Name")) {
if(objects.getString(k).equals("Camera")) {
System.out.println("nKey : " + k + "nName : " + objects.getString(k) + ", nValue : " + objects.getInt("Value"));
}
}
}
}
add a comment |
If you want to get the value in a key-value pair in a JSON you need to use the following syntax:
[JSONObject].get[data_type]([key_name])
.
In the above given syntax, replace the
[JSONObject]
with the
variable of typeJSONObject
representing the JSON. In your case,
it isobjects
.
replace
[data_type]
with the data type of the value at that particular key. In the
current case, value for the key"Value"
is80000
is anInteger
.. Hence it
should begetInt
.
Replace
[key_name]
with the key whose value you need to retrieve, which in your
case is"Value"
.
Hence the code snippet you need to use to get the value 80000 which with the "Camera" part is: objects.getInt("Value")
.
Here is your overall updated code:
JSONObject arrayOfProducts = jsonObj.optJSONObject("Products");
JSONArray products = arrayOfProducts.getJSONArray("Product");
for (int i = 0; i < products.length(); i++) {
JSONObject objects = products.getJSONObject(i);
Iterator key = objects.keys();
while (key.hasNext()) {
String k = key.next().toString();
if(k.equals("Name")) {
if(objects.getString(k).equals("Camera")) {
System.out.println("nKey : " + k + "nName : " + objects.getString(k) + ", nValue : " + objects.getInt("Value"));
}
}
}
}
If you want to get the value in a key-value pair in a JSON you need to use the following syntax:
[JSONObject].get[data_type]([key_name])
.
In the above given syntax, replace the
[JSONObject]
with the
variable of typeJSONObject
representing the JSON. In your case,
it isobjects
.
replace
[data_type]
with the data type of the value at that particular key. In the
current case, value for the key"Value"
is80000
is anInteger
.. Hence it
should begetInt
.
Replace
[key_name]
with the key whose value you need to retrieve, which in your
case is"Value"
.
Hence the code snippet you need to use to get the value 80000 which with the "Camera" part is: objects.getInt("Value")
.
Here is your overall updated code:
JSONObject arrayOfProducts = jsonObj.optJSONObject("Products");
JSONArray products = arrayOfProducts.getJSONArray("Product");
for (int i = 0; i < products.length(); i++) {
JSONObject objects = products.getJSONObject(i);
Iterator key = objects.keys();
while (key.hasNext()) {
String k = key.next().toString();
if(k.equals("Name")) {
if(objects.getString(k).equals("Camera")) {
System.out.println("nKey : " + k + "nName : " + objects.getString(k) + ", nValue : " + objects.getInt("Value"));
}
}
}
}
answered Nov 29 '18 at 6:55
RaiRai
1,1444823
1,1444823
add a comment |
add a comment |
Your JSON is wrong.
- Don't use left/right quotes. Instead use quotes without a direction.
Correct: "Products"
Wrong: “Products”
- Each JSON object should be enclosed in
{}
. Enclose your whole JSON file content within{}
.
add a comment |
Your JSON is wrong.
- Don't use left/right quotes. Instead use quotes without a direction.
Correct: "Products"
Wrong: “Products”
- Each JSON object should be enclosed in
{}
. Enclose your whole JSON file content within{}
.
add a comment |
Your JSON is wrong.
- Don't use left/right quotes. Instead use quotes without a direction.
Correct: "Products"
Wrong: “Products”
- Each JSON object should be enclosed in
{}
. Enclose your whole JSON file content within{}
.
Your JSON is wrong.
- Don't use left/right quotes. Instead use quotes without a direction.
Correct: "Products"
Wrong: “Products”
- Each JSON object should be enclosed in
{}
. Enclose your whole JSON file content within{}
.
answered Nov 29 '18 at 6:36
343GuiltySpark343GuiltySpark
1516
1516
add a comment |
add a comment |
Why you need to choose complex solution to parse JSON like that?
You should use define some entities and then use library for reading JSON value (Ex: "ObjectMapper" of jackson) to parser it to Object.
You can leave all complex parts for them, after that you just working on java object.
add a comment |
Why you need to choose complex solution to parse JSON like that?
You should use define some entities and then use library for reading JSON value (Ex: "ObjectMapper" of jackson) to parser it to Object.
You can leave all complex parts for them, after that you just working on java object.
add a comment |
Why you need to choose complex solution to parse JSON like that?
You should use define some entities and then use library for reading JSON value (Ex: "ObjectMapper" of jackson) to parser it to Object.
You can leave all complex parts for them, after that you just working on java object.
Why you need to choose complex solution to parse JSON like that?
You should use define some entities and then use library for reading JSON value (Ex: "ObjectMapper" of jackson) to parser it to Object.
You can leave all complex parts for them, after that you just working on java object.
answered Nov 29 '18 at 7:20
user2414557user2414557
97
97
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%2f53532730%2fhow-to-retrieve-data-from-json-object-in-java%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
1
Possible duplicate of How to parse JSON in Java
– Dang Nguyen
Nov 29 '18 at 6:03
Your code look normal. But your JSON is not good. Strange quotes and JSON object must begin with "{" and end with "}". How do you create jsonObj? What error do you see? P.S. Your can take "Name" like this: objects.getString("Name") and check "Name" existing (if you need) like this: objects.has("Name")
– Anatoly Samoylenko
Nov 29 '18 at 6:20
This is a part of json. jsonObj contains entire json through which I'm trying to parse the json. Edited quotes
– fervent
Nov 29 '18 at 6:24
Can you please edit the above code to retrieve the value of Camera i.e., 80000 ?
– fervent
Nov 29 '18 at 6:25
replace System.out.println("Key : " + k + ", value : " + objects.getString(k)); on System.out.println("Key : " + k + ", name : " + objects.getString(k) + ", value : " + objects.getInt("Value"));
– Anatoly Samoylenko
Nov 29 '18 at 6:31