This question already has answers here:
Retrieving JSON from URL on Android
(6 answers)
Closed 8 years ago.
I need to use a JSON api, but don't know how to do so. All the tutorials i have found are about parsing json from a file.
I need to make a request like this: http://expandurl.appspot.com/expand?url=http%3A%2F%2Ft.co%2FgLP0Ucg
and then it will return this
{"status": "OK", "end_url": "http://www.systemcomic.com/2011/06/27/the-system-506-office-forecast/", "redirects": 3, "urls": ["http://t.co/gLP0Ucg", "http://www.notquitewrong.com/rosscottinc/2011/06/27/the-system-506-office-forecast/", "http://www.systemcomic.com//2011/06/27/the-system-506-office-forecast/", "http://www.systemcomic.com/2011/06/27/the-system-506-office-forecast/"], "start_url": "http://t.co/gLP0Ucg"}
I just need the end_url string. How would i do this? I am also going to be doing this in a ASyncTask
For getting the JSON you need to extract it using http request and after you get the response you then parse it to get the end_url.
try this to parse end_url:
final Pattern pattern = Pattern.compile("\"end_url\": \"(.+?)\",");
final Matcher matcher = pattern.matcher(You_json_string);
matcher.find();
Log.d("end_url" , matcher.group(1));
Related
This question already has answers here:
How to convert jsonString to JSONObject in Java
(20 answers)
How to convert Java String to JSON Object
(5 answers)
Closed 4 years ago.
I am currently trying to Automate a API using HTTP client jars. I have stored the response of API which is in JSON format into a Java String object.
As next step, I would like to convert the response into a JSON Object, which would help me to easily traverse the object. Can you please let me know which API jars or additional libraries I can use to convert the String object into JSON Object.
apiOutput contains the response which is in the below format.
String apiOutput= {
"title": "foo",
"body": "bar",
"userId": 1,
"id": 101
}
I would like to convert this response into a JSON object to traverse easily.
Simply like this :
import org.json;
JSONObject obj = new JSONObject(jsonString);
This question already has answers here:
Parse a URI String into Name-Value Collection
(30 answers)
Closed 4 years ago.
In selenium, is there a way to grab partial dynamically changing data from a URL? Example, I ran driver.getCurrentUrl() in my code and retrieved the below URL. I only want to grab the numeric portion of it and concatenate it so I end up with 819499-1. However, next time I run it the values will be different. Is there a way to capture this information without capturing the entire URL?
http://custmaint/clnt_estimate_overview.jsp?estimateNbr=819499&versionNbr=1&estimateMgmt=true&clntAutoAssign=true
I think this is a more generic question, not only related to Selenium...
Basically, You could use regular expression matching like so:
String url = "http://custmaint/clnt_estimate_overview.jsp?estimateNbr=819499&versionNbr=1&estimateMgmt=true&clntAutoAssign=true";
final Pattern p = Pattern.compile("^.*\\?estimateNbr=(\\d+)&versionNbr=(\\d+).*$");
Matcher m = p.matcher(url);
if (m.find()) {
System.out.print(m.group(1) + "-" + m.group(2));
}
Another approach (more flexible) is to use a dedicated library like httpcomponents; see How to convert map to url query string?
String url = driver.getCurrentUrl();
String estimateNbr = url.substring(url.indexOf("eNbr=")+5, url.indexOf("&v"));
String versionNbr = url.substring(url.indexOf("nNbr=")+5, url.indexOf("&e"));
System.out.println(estimateNbr + "-" + versionNbr);
This question already has answers here:
How to do URL decoding in Java?
(11 answers)
Closed 7 years ago.
I'm downloading a JSON from the Google Directions API. For the HTML_Instructions field, representing the actual instruction needed for navigation, here is the format:
"Head \u003cb\u003esoutheast\u003c/b\u003e on \u003cb\u003eMinor Ave\u003c/b\u003e toward \u003cb\u003eMadison St\u003c/b\u003e",
Is there a way to decode/remove the escape characters from the String that is downloaded in Java/an Android application.
Thanks for the help.
In Java use
String result = java.net.URLDecoder.decode(url, "UTF-8");
In JS use decodeURIComponent
document.write(decodeURIComponent("Head \u003cb\u003esoutheast\u003c/b\u003e on \u003cb\u003eMinor Ave\u003c/b\u003e toward \u003cb\u003eMadison St\u003c/b\u003e"))
This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 8 years ago.
How to parse following json string in java. I would like to retrieve one by one.
{"IN1005*1001302*CWH":[{"qtyreceived":"5","itemcode":"1618306"},{"qtyreceived":"0","itemcode":"1618305"},{"qtyreceived":"0","itemcode":"288242"]}
Output can be
head=IN1005*1001302*CWH
qtyreceived=5
itemcode:1618306
and so on
Please any one help me . I saw many examples but none of them are matching with my requirement. If you found solution for my question in another old post please let me know the post details and url
You could use GSON, check: https://code.google.com/p/google-gson/
Possible duplicate of JSON parsing using Gson for Java
I would like to suggest an algorithm:
1>Use any good REST API for java. I would suggest Apache Jersey.
2>You will have a Resource Layer in your application in which you will recieve this JSON String from the other layer.
3> You can refer to the example as such in http://www.vogella.com/tutorials/REST/article.html
4>Now the method that you are using will recieve the JSON String as argument. That can be consumed using #XMLROOTElement tag.
Note: you need to convert IN1005*1001302*CWH to IN10051001302CWH or something useful as it has special characters and you cannot create a java class.
getJSON(WSObject object){}
Some description of your java object.
#XmlRootElement
WSObject{
List<IN10051001302CWH> jsonObject;
}
And IN10051001302CWH would be again another object having properties that of your JSON properties which have same spelling :-
#XmlRootElement
IN10051001302CWH{
private Long qtyrecieved;
private Long itemcode;
}
You can achieve this using Jackson, see this Link
Here my answer for converting an object into json format.
This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 9 years ago.
I want to validate url started with http/https/www/ftp and checks for /\ slashes and checks for .com,.org etc at the end of URL using regular expression. Is there any regex patttern for URL validation?
This works:
Pattern p = Pattern.compile("(#)?(href=')?(HREF=')?(HREF=\")?(href=\")?(http://)?[a-zA-Z_0-9\\-]+(\\.\\w[a-zA-Z_0-9\\-]+)+(/[#&\\n\\-=?\\+\\%/\\.\\w]+)?");
Matcher m = p.matcher("your url here");
I am use the following code for that
String lRegex = "^(https?|ftp|file)://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*[-a-zA-Z0-9+&##/%=~_|]";
btw a search in google and you would find the solution by yourself.