I want to use dart's uri to parse the following url, the value in the parameter contains a ”#“
String url = 'scheme://a.b.c/d?p={"url": "https://test.test.com/#/a/b"}';
Uri uri = Uri.parse(url);
var map = uri.queryParameters;
print(map);
I want to get the value of parameter p through uri analysis
The result of the above code is as follows:
{p: {"url": "https://test.test.com/}
The desired result is as follows:
{p: {"url": "https://test.test.com/#/a/b}
Thanks!!!
You can try this. First, you replace # with a unique value. After parsing then again replace the hashtag.
String url = 'scheme://a.b.c/d?p={"url": "https://test.test.com/#/a/b"}';
Uri uri = Uri.parse(url.replaceAll("#","hashtag"));
var map = uri.queryParameters;
print(map.toString().replaceAll("hashtag","#"));
output: {p: {"url": "https://test.test.com/#/a/b"}}
The hash # must be replaced with its encoded value %23:
String url = 'scheme://a.b.c/d?p={"url": "https://test.test.com/%23/a/b"}';
The URL you supplied should have had characters with special meanings escaped/encoded in the first place. Uri.parse probably is parsing it correctly; you just gave it malformed input for what you intended.
What you should do instead is to encode the argument for your query string first:
String queryArgument = '{"url": "https://test.test.com/#/a/b"}';
String encodedArgument = Uri.encodeQueryComponent(queryArgument);
String url = 'scheme://a.b.c/d?p=$encodedArgument';
Uri uri = Uri.parse(url);
var map = uri.queryParameters;
print(map); // Prints: {p: {"url": "https://test.test.com/#/a/b"}}
Related
The official Turkey's Dictionary has some kind of json request system but the response it gave is HTML, not JSON.
Example:
https://sozluk.gov.tr/gts?ara=kalem
I'm trying to convert this HTML to JSON but couldn't make it. When I use html plugin on Flutter it gives me some kind of #document every time.
var sozlukurl = await http.Client()
.get(Uri.parse('https://sozluk.gov.tr/gts?ara=$ceviri'));
print(sozlukurl);
var sozlukapibody = parse(sozlukurl.body);
print(sozlukapibody);
var decoded = json.decode(sozlukapibody.toString());
var sozlukbilgi = jsonDecode(utf8.decode(decoded.bodyBytes)) as Map;
var sozlukanlam = sozlukbilgi['anlamlarListe'][0]['anlam'];
print(sozlukanlam);
Output from sozlukurl:
I/flutter ( 5350): Instance of 'Response'
Output from sozlukapibody:
I/flutter ( 5350): #document
Final Error:
FormatException (FormatException: Unexpected character (at character 1)
#document
^
)
How can i solve this problem?
It returns a json with an array encapsulating it. For your particular example jsonDecode should work fine, just take index 0 of the array to access the json.
var res = await http.Client()
.get(Uri.parse('https://sozluk.gov.tr/gts?ara=kalem'));
var body = res.body;
var decoded = jsonDecode(body);
var json = decoded[0];
var sozlukanlam = json["anlamlarListe"][0]["anlam"];
print(sozlukanlam);
I tried it on DartPad and not on an actual app however.
Given a name of a file like this:
let fileName = "example.jpg"
I am grabbing the URL of this directory using a helper function, which returns the URL to me:
let imagesURL = getURL()
How can I append the string fileName to the imagesURL such that it returns something like this:
/path/to/images/dir/example.jpg
How do I get it in both String and URL format?
This gives you a new URL with the fileName appended:
let appended = imagesURL.appendingPathComponent(fileName)
And this converts it back to a string:
let strVersion = appended.absoluteString //full URL
let strVersion2 = appended.path //path only
url.appendingPathComponent(fileName)
has been deprecated, use
url.appending(path: filename) instead.
I have an input string "+20" and I am trying to pass that as query parameter in url.
So I am trying to encode the myInputString by doing
let s1 = myInputString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
But in the debugger, the string s1 still shows as '+20' instead of '%2B20'
Is there something I did wrong?
As already mentioned by matt + is a legal URL character. If you really need to encode it you would need to create your own custom urlQueryAllowed and subtract the plus sign from it:
extension CharacterSet {
static let allowedCharacters = urlQueryAllowed.subtracting(.init(charactersIn: "+"))
}
let myInputString = "+20"
let s1 = myInputString.addingPercentEncoding(withAllowedCharacters: .allowedCharacters) // "%2B20"
//main code
list ls = memberMap.get(Rz.Roles__c);
String JSONString = JSON.serialize(ls);
//Debug code
[{"attributes":{"type":"Work_Team_Member__c","url":"/services/data/v42.0/sobjects/Work_Team_Member__c/a81W00000008gIsIAI"},"Id":"a81W00000008gIsIAI","Member_Employee_ID__c":"63","Member_Name__c":"Test1","Member_Role__c":"Account Representative – General (Secondary)","Work_Team_Master__c":"a80W00000009DodIAE"}]
How to convert String like "fff\"fff" to URL?
URL must contain " character and be like "ff"ff"
You escape the " character to %22. The easiest way to do that is via URLComponents:
var components = URLComponents(string: "https://somewhere.com")!
components.query = "ff\"ff"
let url = components.url! // https://somewhere.com?ff%22ff