This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 5 years ago.
Okay so I'm trying to parse the url and extract partial data.. However it doesn't seem to be extracting the exact data I want.. I assume it's extracting ID and not Value.
This is the code I'm using
price = readUrl(apiUrl + String.valueOf(id)).split(",")[1].split(":")[1];
String price2 = price.substring(0, price.length() - 1);
return Integer.parseInt(price2);
the url I'm using is
https://api.rsbuddy.com/grandExchange?a=guidePrice&i=
parameter i = id of item, for this example we will use " 2619 "
which returns,
{"overall":49907,"buying":0,"buyingQuantity":0,"selling":49907,"sellingQuantity":2}
the information I want is
49907
from
{"overall":49907,
Use JSONObject:
JSONObject jsonObject = new JSONObject(YOUR_STRING_YOU_WANT_TO_PARSE);
Integer price = jsonObject.getInt("overall");
What you get from API is a JSON. So you can simply use JSONObject.
https://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html
You can do something like:
jsonObject.getInt("overall");
Related
This question already has answers here:
How to parse or split URL Address in Java?
(4 answers)
Closed 3 years ago.
I want to get "BusinessId" from this URL in Java: https://example.com/?link=https://play.google.com/store/apps/details?id=com.example.cp&hl=es&apn=com.picker.cp&st=Share+this+app&utm_source=AndroidApp?businessId=5d8648b561abf51ff7a6c189
What can i do?
I need some help, please :C
You could try using String#replaceAll for a one-line solution:
String url = "https://example.com/?link=https://play.google.com/store/apps/details?id=com.example.cp&hl=es&apn=com.picker.cp&st=Share+this+app&utm_source=AndroidApp?businessId=5d8648b561abf51ff7a6c189";
String businessId = url.replaceAll(".*[&?]businessId=([^=?&]+)\\b.*", "$1");
System.out.println(businessId);
This prints:
5d8648b561abf51ff7a6c189
Actually Apache has a number of libraries that can make handling your requirement much easier. On Android, the following might work:
Uri uri = Uri.parse(url);
String linkParam = uri.getQueryParameter("link");
Uri uri2 = Uri.parse(linkParam);
String businessId = uri2.getQueryParameter("businessId");
I noticed android tag in your question. You can parse the Url string to Uri and then you can get query param.
String myUrl = "https://example.com/?link=https://play.google.com/store/apps/details?id=com.example.cp&hl=es&apn=com.picker.cp&st=Share+this+app&utm_source=AndroidApp?businessId=5d8648b561abf51ff7a6c189";
Uri uri = Uri.parse(myUrl);
String businessId = uri.getQueryParameter("businessId");
This question already has answers here:
How to parse or split URL Address in Java?
(4 answers)
Closed 6 years ago.
I have a URL like this:
http://www.chalklit.in/post.html?chapter=V-Maths-Addition%20&%20Subtraction&post=394
How to get the value of parameter of chapter and post?
My URL contains '&' in the value of chapter parameter.
You can use the Uri class in Android to do this; https://developer.android.com/reference/android/net/Uri.html
Uri uri = Uri.parse("http://www.chalklit.in/post.html?chapter=V-Maths-Addition%20&%20Subtraction&post=394");
String server = uri.getAuthority();
String path = uri.getPath();
String protocol = uri.getScheme();
Set<String> args = uri.getQueryParameterNames();
Then you can even get a specific element from the query parameters as such;
String chapter = uri.getQueryParameter("chapter"); //will return "V-Maths-Addition "
I have this JSON data
"posts":[{
"date":"2016-02-10 10:28:42",
"categories":[{}],
"tags":[],
"author":{"name":"admin"},
"custom_fields":{
"ref_number":["ITB NUMBER: ITB\/002\/2016"],
"deadline":["26 February, 2016"],
"entity":["Refugees (xxx)"]
}
I want to pass entitywith the code below in my JSONParser.java
Post post = new Post();
// Configure the Post object
post.setTitle(postObject.optString("title"));
post.setDate(postObject.optString("date", "N/A"));
post.setContent(postObject.optString("content", "N/A"));
post.setCfs(postObject.getJSONObject("custom_fields").optJSONArray("entity").getString(0));
to my Webview. using the code below in my PostFragment.java
id = args.getInt("id");
//Title and date pass successfully
title = args.getString("title");
String date = args.getString("date");
//but the entity displays null
entity = args.getString("entity");
//author is passed and it displays well/successfully
String author = args.getString("author");
// Construct HTML content
// html for entity to webview
html += "<h2>" + entity + "</h2>";
html += "<h3>" + title + "</h3>";
// The actual content
html += content;
am able to pass and display title and content, however when i try entity it shows null in webview
Where am i going wrong
Try:
entity = args.getString("custom_fields.entity");
Hope it helps!
Your question is not clear , as per my understanding you need Refugee element
The JSON you have posted is not valid.
As per my understanding elements of entity is also an JSONObject , so the parser should be like this
.optJSONArray("entity").getJSONObject(i); // i is poistion
This will give you ith element of entity array
This question already has answers here:
Parsing result of URL.getHost()
(2 answers)
Closed 8 years ago.
I need to parse url in my java code and get the domain. I wrote the following code:
static String domain(URL url) {
String host = url.getHost();
int i = host.lastIndexOf('.');
if(i == -1){
return "Not domain";
}
if (i ==0 ){
return "Not domain";
}
String domain;
i = host.lastIndexOf('.', i - 1);
if (i == -1) {
domain = host;
}
else {
domain = host.substring(i + 1, host.length());
}
}
This code parses domains like example.com
But how can my code parse domains like exmaple.co.ir , subdomains.example.co.ir and the others extensions like co.uk, org.ir and so on.
EDIT
my url is http//blog.example.co.ir/index.php or http//blog.example.co.uk/something.html
my goal is to print:
example.co.ir and example.co.uk
The problem is that your parsing code is limited to domains with just one dot. You can use regular expressions or recursive parsing to solve this problem. This is one way of approaching this problem.
I believe this work for any kind of URL(in correct URL format)
domain= host.split("/")[2];
Note:
split("/") will create an array from the String, for example:
String host="http//blog.example.co.ir/index.php";
host.split("/") will give you array of String: [http, ,blog.example.co.ir, index.php]
And your desired output is at index 2
I'm trying to parse JSON data from a google maps search.
I've tryed both JACKSON and and now I'm Trying JSON SIMPLE. Both of them gives the same error.
First of all I'm doing an search on Google maps.
String urlString = "http://maps.google.com/maps?f=q&source=s_q&output=json&start=0&q="+ "Stockholm" + "+Gym";
Gives me JSON while(1);{title:"stockholm Gym - Google Maps",url:"/maps?f=q\x26source=s_q\x26start=0\x26q=stockholm+Gym\x26ie=UTF8\x26hq=Gym.............. and so on.
I'm replacing the while(1); with ""; before i return the string.
To the problem when I'm trying to parse it
JSONParser parser = new JSONParser();
String jsonString = "";
// UriHandler.mapSearchJson is the method that returns the jsonString.
String jsonData = UriHandler.mapSearchJSON(jsonString);
Object obj = "";
try {
obj = parser.parse(jsonData);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject jsonObj = (JSONObject) obj;
String title = (String) jsonObj.get("title");
System.out.println(title);
This gives me the exception.
Unexpected character (t) at position 2.
When I'm debbuging it. comes all the way to when it's trying to parse the string. then the obj is = null.
What in thw world am I doing wrong.
Thanks!
As the others already mentioned, a nonquoted field name is not standard JSON. However, Jackson (and maybe others) has a set of option settings that allow it to work with nonstandard, but common JSON derivatives:
JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES
will enable processing of unquoted field names.
The response is not valid JSON, as the key name was not quoted with double quotes.
{title:"stockholm Gym"
is invalid JSON, it should be this:
{"title":"stockholm Gym"
Notice how title is surrounded by " double quotes
You are pulling back Javascript code that is meant for the maps.google.com site to use.
There could be any Javascript code in that response, not just the JSON that happens to be returned as part of the search.
You need to request from their maps API instead:
http://maps.googleapis.com/maps/api/geocode/json?address=Stockholm+Gym&sensor=false
This will return you only the JSON data.
Have a look the Google Maps API for more options.
I faced this error when trying to parse the json returned from kafka (kafka twitter producer).
The message returned was including some extra text other than json (KeyedMessage(twitter-test_english,null,null). Because of that I was facing this error.
KeyedMessage(twitter-test_english,null,null,{"created_at":"Sat Apr 23 18:31:10 +0000 2016","id":723942306777337856,"id_str":"723942306777337856"}
Pass only the message part from returned json and convert it into string.
{"created_at":"Sat Apr 23 18:31:10 +0000 2016","id":723942306777337856,"id_str":"723942306777337856"}
message = new KeyedMessage("twitter-test_english", (String)queue.take());
//System.out.println("This is message"+message.message());
String message_string = message.message().toString();
JsonParse.toParseJson(message_string);