at the moment I'm parsing the string with eval()-method and it works fine, but want to parse it with the native JSON methods of a browser. Is there good way to parse the string with jquery.parseJSON() or with any other jQuery method to an array of javascript objects? The string looks like
[
{
"title": "01 Books",
"groupId": "01",
"groupName": "Books",
"key": "1",
"isFolder": true,
"expand": false,
"isLazy": true
},
{
"title": "02 Music",
"groupId": "02",
"groupName": "Music",
"key": "2",
"isFolder": true,
"expand": false,
"isLazy": true
}
]
build with net.sf.json.JSONArray of Java.
jquery.parseJSON() can just parse strings like {"Key":"Value","Key":"Value",....} I guess?
thx for your help!
You have a typo. It is jQuery, not jquery. Capitalization is important.
jQuery.parseJSON()
or
$.parseJSON()
This is supposed to parse it into a json object.
Related
Dears,
I am working on creating a simple method which will take String argument which will be a path or other kind "pointer" to attribute/s in JSON and this method will remove those attribute/s.
My problem is I can find values of those attribute/s using JsonPath, but I can't find methods in rest assured (or other libraries) which could remove/delete attributes by given path.
JSON is already added earlier so i need to pull him from RequestSpecification or FilterableRequestSpecification object ex.
RequestSpecification rs = *objFromContext*;
FilterableRequestSpecification frs= (FilterableRequestSpecification) rs;
frs.getBody();
I've tried to work with JSONObject class and remove() but it doesn't work on complex JSONs.
given example JSON
{
"created": "string",
"updated": "string",
"items": [
{
"code": "TEST",
"nested": {
"code": "test",
"name": "name",
"other": [
{
"code": "TEST",
"name": "myName",
"quantity": 1
}
]
},
"itemsProperties": [
{
"code": "value1",
"name": "name",
"value": 123
}
]
},
{
"code": "TEST",
"nested": {
"code": "test",
"name": "name",
"other": [
{
"code": "TEST",
"name": "myName",
"quantity": 1
}
]
},
"itemsProperties": [
{
"code": "value2",
"name": "name",
"value": 123
}
]
}
],
"timer": {
"startDate": "2015-01-01",
"endDate": "2021-01-02"
},
"id": "myId"
}
using JsonPath jp = JsonPath.from(httpRequest.getBody().toString());
and then jp.get(items.itemsproperties.code) i can find value1 and value2.
I stuck in this point: How to remove those attributes from sended body?
I know i can convert body into JSONObject and then go field after field conversion between getJSONArray and GetJSONOBject and remove those fields, but i would like to make this metod much more universal.
Is this possible?
If you want to manipulate json in Rest-Assured JsonPath, then the answer is No. You can't do that. JsonPath help you to extract value from json, that's it, no more.
You have to use different libraries to remove key-value pair.
For example: using JsonPath Jayway
DocumentContext parse = JsonPath.parse(body);
parse.delete("$..itemsProperties..code");
System.out.println(parse.jsonString());
This is my sample JSON Response:
{
"success": true,
"details": {
"banners": [
{
"banner_id": "1",
"file_id": "368",
"title": "INANG KALIKASAN'S BAD HAIR DAY",
"description": "Et aeterno partiendo his, vim ponderum abhorreant et. Eum debet recusabo repudiandae in.",
"created": "2018-03-09 07:56:04",
"modified": "2018-04-02 09:59:54",
"deleted": "",
"file_name": "https://globegfs.imgix.net/uploads/2018-04-02/8a90371e9ad33cf06848b354b1fbd795.jpg",
"orig_file_name": "hero-1.jpg"
}
],
"recent_books": [
{
"content_id": "353",
"category_id": "24",
"title": "One Hundred Fourth Symphony, in D-major",
"author": "Franz Josef Haydn",
"description": "The last of the 12 London Symphonies written by Franz Haydn. This ebook contains copies of the 4 movements in .mid format.",
"tags": [
{
"content_id": "353",
"tag_id": "14",
"created": "2018-04-27 15:57:46",
"tag_name": "Ray Collections"
}
]
}
],
"classics": [
{
"content_id": "353",
"category_id": "24",
"title": "One Hundred Fourth Symphony, in D-major",
"author": "Franz Josef Haydn",
"description": "The last of the 12 London Symphonies written by Franz Haydn. This ebook contains copies of the 4 movements in .mid format.",
"tags": [
{
"content_id": "353",
"tag_id": "14",
"created": "2018-04-27 15:57:46",
"tag_name": "Ray Collections"
}
]
}
]
}
}
Only the banners object has a different list of contents. All the succeeding Json Elements will have the same contents but in a list, having its Json Node its actual title. How can I map this into a HashMap?
I figured it will look something like this:
private List<GFSBannerResponse> banners;
private List<HashMap<String, List<GFSContentResponse>>> featuredContent;
but I still don't have an idea on how to implement this. I am using Retrofit 2.0 and Gson.
I ended up implementing it this way:
#SerializedName("details")
#Expose
private JsonElement details;
I just used JsonParser to parse the content to a JsonElement. Stuck with GSON for all of this, although this wasn't the ideal implementation. Will try to implement a custom Deserializer in the future.
Please help me with correct json path. I am trying to extract "500" from "value".
Json (part of it) looks like this:
Vehicle {
"code": "BCA",
"name": "COLL",
"description": "Collision",
"limitTerms": [],
"deductibleTerms": [
{
"code": "qsw",
"name": "",
"value": "500",
"valueCode": "",
"valueDescription": "",
}
],
"otherTerms": [],
},
I want to use the name or description than reach to deductibleTerms and extract value from it.
I tried wrtting json path like that (which is for sure wrong)
"$.vehicle[description='Collision' and .deductibleTerms[*].value]"
The .deductibleTerms[*].value should not be inside the filter [] part of the expression since you are not filtering on it.
$.vehicle[description='Collision'].deductibleTerms[*].value
I am developing first time in android and i have never used json data before. I will develop an application of event calendar of my university. We developed web version application in Django and we implement tastypie (restapi) so i need to use this json data for android mobile version. My json data is like this :
{
"meta": {
"limit": 20,
"next": null,
"offset": 0,
"previous": null,
"total_count": 5
},
"objects": [{
"Location": "Z011",
"Notes": "asdf",
"Title": "Literature Talking",
"id": 3,
"resource_uri": "/api/v1/Events/3/"
}, {
"Location": "Batı Kampüsü, Sinema Salonua",
"Notes": "sd",
"Title": "TARİHÇE KONFERANSLARI SERİSİ 25",
"id": 4,
"resource_uri": "/api/v1/Events/4/"
}, {
"Location": "in Campus",
"Notes": "afafdf",
"Title": "Self-Assessment Project",
"id": 5,
"resource_uri": "/api/v1/Events/5/"
}, {
"Location": "Kütüphane",
"Notes": "fs",
"Title": "51.Kütüphane Haftası",
"id": 6,
"resource_uri": "/api/v1/Events/6/"
}]
}
how can I parse this Json data in android studio?
Using below code you will be able to get Title and Location
JSONObject obj=new JSONObject(response);//This is response from webservice
String totalCount = obj.getJSONObject("meta").getString("total_count"); //for getting total_count
JSONArray json_array = obj.getJSONArray("objects");
for(int j=0;j<json_array.length();j++) {
String title = json_array.getJSONObject(j).getString("Title");
String location= json_array.getJSONObject(j).getString("Location");
}
Use this website to help you view the Json structure better
http://www.jsontree.com/
What you have is a Json Object since it starts and ends with curly braces.
For example if I had a Json as {"Id":"1"}
The Key is "Id" and the value is "1"
A Json object can have a Json inside the value as well(Which is your case)
And example is {"Id":{"Place1":"1", "Place2":"2"}}
So the Key is "Id" and it has the value "Place1":"1", "Place2":"2"
So the value is also a Json.
It can get a little messy with Jsons in Jsons.
Here is a good tutorial on parsing Json
http://www.tutorialspoint.com/android/android_json_parser.htm
This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 5 years ago.
I'm trying to parse an output from a server that looks like this:
{
"GetFolderFilesByZoneResult": [
{
"ID": 98748,
"CreatedBy": "",
"UpdatedBy": "none",
"CreatedDate": "\/Date(1308273033620+0100)\/",
"UpdatedDate": "\/Date(1308303003770+0100)\/",
"CommentCount": 0,
"Key": "",
"Enabled": true,
"MimeType": "video",
"Votes": 2,
"TotalRating": 0,
"AllowComments": true,
"ViewCount": 323,
"ReleaseDate": "\/Date(1308273000000+0100)\/",
"ExpireDate": "\/Date(4102444800000+0000)\/",
"Author": "",
"Size": 133799936,
"Tag1": "",
"Tag2": "",
"Tag3": "",
"RecycleBin": false
},
{
"ID": 99107,
"CreatedBy": "",
"UpdatedBy": "none",
"CreatedDate": "\/Date(1308583412520+0100)\/",
"UpdatedDate": "\/Date(1308583564007+0100)\/",
"CommentCount": 0,
"Key": "",
"Enabled": true,
"MimeType": "video",
"Votes": 0,
"TotalRating": 0,
"AllowComments": true,
"ViewCount": 33,
"ReleaseDate": "\/Date(1308583380000+0100)\/",
"ExpireDate": "\/Date(4102444800000+0000)\/",
"Author": "",
"Size": 47955968,
"Tag1": "",
"Tag2": "",
"Tag3": "",
"RecycleBin": false
}
]
}
I'm trying to use Java org.json to parse it, but I don't have any experience with JSON/org.json, so I'm having a little trouble. How can I parse this?
1) Assuming you have the JSON libraries on your path (from www.json.org), it's pretty easy.
import org.json.JSONTokener;
...
URI uri = new URI("http://someserver/data.json");
JSONTokener tokener = new JSONTokener(uri.toURL().openStream());
JSONObject root = new JSONObject(tokener);
From there, you can address the various parts of the JSON object. Take a look at the Javadocs for the specifics.
https://developer.android.com/reference/org/json/package-summary.html
Here is the most universal solution, which allows to parse any JSON type into appropriate Java type:
Object json = new JSONTokener(response).nextValue();
Then you can determine resulting type and handle it appropriately.
I'd pass it
map<String, Object>
loaded with
map<String, Object>
in the object field.
Basically recreating the hierarchy of your java classes inside of a large map.
Example :
return ( Map<"GetFolderFilesByZoneResult", Map<"Result1", (object by id 98748) | "Result2", (object by id 99107) | "Result3", etc.
JSON will return that big map very pretty like, and programmatically it's easier to do then lists.