Json String to Map<> or List - dart

I have a json stored as string like below
String json="[{"name":"a","id",1},{"name":"b","id",2},{"name":"c","id",3}]";
My Question how to encode this to a map or a list to get access to the keys and use the values?

You need to JSON-decode the value first
import 'dart:convert';
final decoded = jsonDecode(json);
print(decoded[0]['name']); // just one example

Related

QAF -how to print /access common step Store () value into variable

I am new to QAF. I m using common steps to store value into one variable and need to assert one locator has that value.
store(Object val, String var);
store(2000, “currnetBalance”);
I need to do below activity.
System.out.println(${varname})
How to pass asserttext(“//#[availableText]”, ${varname})
I tried to pass single quotes, double quotes, concatenate string not able to print result.
you can access stored variable from configuration manager. Use ConfigurationManager.getBundle() to access them in code. For example:
import static com.qmetry.qaf.automation.core.ConfigurationManager.getBundle;
String val = getBundle().getString("varname");
Object val = getBundle().getObject("varname");

Dart cast using wrong type?

I'm new to Dart and was wondering over how the .cast() method works with dynamic types and lists.
This is a working example from the Flutter documentation on how to parse JSON manually in Dart:
List<Photo> parsePhotos(String responseBody) {
final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();
return parsed.map<Photo>((json) => Photo.fromJson(json)).toList();
}
where responseBody is some JSON array previously fetched from a HTTP endpoint.
I don't understand why the result of json.decode(responseBody) is cast to Map<String, dynamic> when logically it should be List<Map<String, dynamic>>. I've debugged the code and in fact the variable parsed is a list subtype.
What am I getting wrong here?
Thanks in advance.
It looks like it is correct. cast is a method of Iterable. The type in angle brackets is the type of each element in the iterable.
https://api.dart.dev/stable/2.7.1/dart-core/Iterable/cast.html

How to create a new data with both string part and a data in bytes, in Swift?

I am making an app which behaves like cloud storage. I need to upload a file with Alamofire. When I am uploading the related file, I need to append a string which holds path like: "folderName/fileName" (which shows the path on my cloud storage app) to my file data in bytes. I can achieve this by using below code in Java:
#Override
protected Map<String, DataPart> getByteData() {
Map<String, DataPart> params = new HashMap<>();
params.put("parameterName", new DataPart("folderName/fileName", fileInBytes));
return params;
}
But I don't know how to do this in Swift. I need to create a new Data with a string and a data, before uploading it with Alamofire. How can I achieve that? Any response will be appreciated.
Thanks everyone who answered me, I've found a solution. I created a structure called DataPart with a string and data, and put that parameter to my Alamofire request with parameter name.
"What is the equivalent of a Java HashMap<String,Integer> in Swift" this entry helped me a lot in case you are wondering.
If I understand correctly, you want to convert your path (of type String) to swift's Data structure, then upload it to your server alongside a joined file's data.
so here's how you can do it-
let path = "folderName/fileName"
let pathEncodedData = path.data(using: .utf8)!
let fileData: Data = #YourFile'sData
let params = ["path": pathEncodedData, "fileData": fileData]

Flutter - Remove escape sequence in dart

To decode API response string to JSON, json.decode() works fine.
This will parse a JSON string similar to
{ "Response" : {"Responsecode" : "1" , "Response" : "Success"}}
But in my case, the response comes in the serialized form like:
{\"Response\" : {\"Responsecode\" : \"0\" , \"Response\" : \"Success\"}}
json.decode() won’t work.
In Java, I used StringEscapeUtils.unescapeJson() for the same problem.
I searched for Dart but couldn’t find how to unescape characters in a string.
Edit:
Suppose, the value of key data is abc"de
So, its corresponding JSON would be {"data":"abc\"de"}
And hence during serialization, this json string is escaped to give {\"data\":\"abc\\\"de\"} as the response, which is sent by the API.
So, my intention is to remove the escape sequences, so that I can get the string {"data":"abc\"de"}, which would later be decoded using json.decode(). Removing the escape sequences was done using StringEscapeUtils.unescapeJson() in java.
json.decode can decode single strings too, so you should be able to just call it twice. The first time it'll return you a string (where the escape characters have been decoded) and the second time it'll decode that string into the map:
import 'dart:convert';
void main() {
var a = r'''"{\"Response\" : {\"Responsecode\" : \"0\" , \"Response\" : \"Success\"}}"''';
var b = json.decode(json.decode(a));
print(b['Response']['Responsecode']); // 0
print(b['Response']['Response']); // Success
}

.NET RSAKeyValue base64 private key to a single base64 private key

I am supplied the following RSA private key in the format
<RSAKeyValue>
<Modulus>XXXXXXXX</Modulus>
<Exponent>XXXXXXXX</Exponent>
<P>XXXXXXXX</P>
<Q>XXXXXXXX</Q>
<DP>XXXXXXXX</DP>
<DQ>XXXXXXXX</DQ>
<InverseQ>XXXXXXXXXX/InverseQ>
<D>XXXXXXXX</D>
</RSAKeyValue>
The XXXX are in Base64 format.
I want to know how to combine it all the XXXXXX bits to a single Base64 string.
With this single Base64 string i do the following:
1. Feed it to a TMemorStream
2. use Indy's TIdDecoderMIME class to decode Base64 from the MemoryStream
3. The decoded MemoryStream is then feed into CryptDecrypt function from wcrypt2.pas (a delphi wrapper of Microsoft's Cryptographic API) from Jedi
I know the solution for public key in the same format
<RSAKeyValue>
<Modulus>xqiYKv0umaLdmrKPyBfYmAfzZYVsvsOJyS4c1lBPjqpn7zh+XyxPXK7MxJkAlenQJM33M+ZYfmlPLya7JWXXTPviylEEtlmul9GshpX2caxWu2YO9vNIHRZYYau4ccbkm95iMyJi8KN2ANtqDwiJv55vcXZDqjPSDE4ap49xmog==</Modulus>
<Exponent>AAQC</Exponent>
</RSAKeyValue>
The solution is to add "BgIAAACkAABSU0ExAAQAAAE" + Exponent + Modulus
The result is:
BgIAAACkAABSU0ExAAQAAAEAAQCxqiYKv0umaLdmrKPyBfYmAfzZYVsvsOJyS4c1lBPjqpn7zh+XyxPXK7MxJkAlenQJM33M+ZYfmlPLya7JWXXTPviylEEtlmul9GshpX2caxWu2YO9vNIHRZYYau4ccbkm95iMyJi8KN2ANtqDwiJv55vcXZDqjPSDE4ap49xmog==
With the private key how do we combine it? I know it starts off like this:
"BwIAAACkAABSU0ExAAQAAAE" + Exponent + Modulus + ???????
The XXXX in the RSAKeyValue XML are in base64, just that i do not want to expose the details there. I want to know how do i combine all the XXXX base64 codes into a single base64 private key.
I suspect that this means that you are performing the base64 encoding line by line. It's much simpler to perform the encoding on the entire file.
For example you might do this as follows:
Load the file into a TStringList.
Extract a single string representing the file using the Text property of the string list.
Base64 encode that string.
Send it over the wire.
At the receiving end, decode the string.
Assign the string to the Text property of a string list.

Resources