This question already has answers here:
How can I convert JSON to a HashMap using Gson?
(16 answers)
Closed 5 years ago.
I have a problem trying to parse a json file to JSON object
My JSON File is under the form :
{ "objectJSON": [
{
"attribute":"value",
"attribute":"value",
"attribute":"value",
"attributes" :{
"att1": "val1",
"att2":"val2",
"att3":"val3",
// more atts and vals may figure here !
}
},
{
"attribute":"value",
"attribute":"value",
"attribute":"value",
"attributes" :{
"att1": "val1",
"att2":"val2",
"att3":"val3",
// more atts and vals may figure here !
}
}
]
}
my problem is that the object "attributes" has an unknown number of arguments so I can not create a class for it , I thought about using a
map<String,String> attributes;
but I don't know how to parse it from the file especially that I want to keep my class "ObjectJSON" witch represents the root of my file.
I'm using gson of google
thanks in advance .
Using org.json library:
JSONObject jsonObj = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
Related
This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 2 years ago.
I'm just getting started with using json with java. I'm not sure how to access string values within a JSONArray. For instance, my json looks like this:
"media": {
"images": [
"https://newstaging-api.safegold.com"
],
"videos": [
"https://newstaging-api.safegold.com"
]
}
Now I want to get the value of "images".Actually I want show image in ImageView from URL.
Thanks in advance
JSONObject json = new JSONObject(json_data); //json_data - your json string
JSONObject media = json.getJSONObject("media");
JSONArray images = media.getJSONArray("images");
String link = images.getString(0);```
This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 4 years ago.
i want to parse this code to get the id's location in an array.
Json is as follows:
"tipSecurableSet": {
"id": "082bdd09-6cff-41f9-a6f6-16a5bcc2eaa6",
"items": [
{
"typeId": "c5831702-15ed-483d-8253-c890e3f97dae",
"ids": [
"44b3efd4-5f7f-4698-aba4-1f4d9605c4eb"
],
"id": "b838aa0d-522a-411e-958a-41c711c6a856"
},
{
"typeId": "01c3c7de-2856-4665-90bc-a614caf335cd",
"ids": [
"8240a190-7e14-4ba4-87be-22febd09fa69"
],
"id": "62e62bf5-7a18-4518-a917-bb908d61fdd2"
}
]
}
}
You should take a look at Org.JSON library.
You can get the IDs very easily once you have your JSONObject. There are many ways to construct a JSONObject. You can parse XML.toJSONObject or use one of the JSONObject constructors.
JSONObject obj= new JSONObject(...);
Iterator<String> keys= obj.keys(); //Gets your Fields or Keys
To get the object for a key, here is an example with a String and an Array of JSONObjects.
String id= jsonArray.getJSONObject(i).getString("typeID")
This question already has answers here:
How should I use servlets and Ajax?
(7 answers)
Closed 6 years ago.
This is my code:
json = JSON.stringify(jsonObj);
$.ajax({
type: 'post',
url: 'PostStudentMarks',
async: false,
data: json,
contentType: "application/json",
dataType: 'json',
success: function (response) {
alert("Marks Uploaded");
},
error: function(err) {
console.log(arguments);
}
});
jsonObj contains data like this
[{
"student_id": "11204172"
},{
"course_id": "PHY101",
"semester": "1",
"marks": "11"
},{
"course_id": "CSE401",
"semester": "2",
"marks": "22"
}]
What should be the proper Java code in the servlet to read and iterate through it?Currently I am using org.json.simple. Moreover if I do like
JSONObject jsonObject = new JSONObject(request.getParameter("json"));
JSONArray json = new JSONArray(jsonObject);
It throws an error saying undefined constructor. What is the proper solution for this? Also note that I do not want to use gson.
You are better to use a java Pojo which will have same property as your json keys contains . eg: student_id,course_id etc.
Then convert the json to your Pojo class accordingly .... Eg:
If your json contain objects the convert to simple pojo
If your json contains array then you can use array list as well
Eg:
ObjectMapper mapper = new ObjectMapper();
String jsonInString = "{'name' : 'gokul'}";
Staff obj = mapper.readValue(jsonInString, Staff.class);
I am a newbie in Java, i'm researching how to parse json to object in java.
I have the following Json content:
{
"objects": [
{
"type": "image",
"left":0,
"top":0,
"width":787,
"height":1165,
"src":"image/16_011020002_000_bk.PNG",
"replaceable":false,
"lockObject":false
},
{
"type": "image",
"left":70,
"top":54,
"width":669,
"height":469,
"src":"image/16_011020002_000_il.PNG",
"replaceable":false,
"lockObject":false
},
{
"left":70,
"top":54,
"width":669,
"height":469,
"direction":"v",
"fontFamily":"KaitiEG4-Medium-SJIS",
"fill":"#55626C",
"text":"旧年中は大変お世話になり\nありがとうございました\n本年も相変わらずご支援のほど\nお願い申し上げます\n\n 平成二十八年 元旦",
"textAlign":"left",
"lockObject":false
},
{
"left":70,
"top":54,
"width":669,
"height":469,
"direction":"v",
"fontFamily":"LeisuEG4-Medium-SJIS",
"fill":"#55626C",
"text":"謹んで\n 初春のお慶びを\n 申し上げます",
"textAlign":"left",
"lockObject":false
}
]
}
How to design an object for this json and how to parse json to that object?
Help me this issue. Thank you!
Use some kind of JSON parser.
GSON
https://github.com/google/gson
or Jackson
http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
I hope it will help you...!
use Jackson-
JSONArray objects=new JSONObject(jsondata).getJSONArray("objects");
for(int i=0;i<objects.length();i++){
JSONObject object=objects.getJSONObject(i);
System.out.println("value of left=="+object.getString("left"));
System.out.println("value of top=="+object.getString("top"));
}
As per your question its seems to be an array representing the json data of type object.
To parse the data we can use the above two parsers mentioned by d__k.
I have been using Jackson and we have a ObjectMapper class which converts the data in the specified type. We can use ObjectMapper#readValue to read the data into java object.
Kindly find more information at this link.
This question already has answers here:
Parsing JSON Object in Java [duplicate]
(5 answers)
Closed 7 years ago.
I have the following JSON text and would like to get the values "detest" and "dislike".
{
"noun": {
"syn": [
"hatred",
"emotion"
],
"ant": [
"love"
]
},
"verb": {
"syn": [
"detest",
"dislike"
],
"ant": [
"love"
]
}
}
I've tried parsing it using the org.json library with the following:
JSONArray obj = new JSONObject(String);
JSONArray arr = obj.getJSONArray("syn");
But I can't seem to access the array this way.
Any help is greatly appreciated.
The following is related to the JSON.simple library.
You need a JSONParser instance to get a JSONObject value from your String object.
JSONObject data;
try {
JSONParser helper = new JSONParser();
data = (JSONObject)helper.parse(String);
} catch (ParseException parse) {
// Invalid syntax
return;
}
// Note that these may throw several exceptions
JSONObject node = (JSONObject)data.get("verb");
JSONArray array = (JSONArray)node.get("syn");