How can i get specific value from JSON string - apex

//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"}]

Related

Flutter/Dart convert a string formated as an object into a array

I am attempting to convert a string that is formatted as an object but with no luck, the data is originally formatted as a _CompactLinkedHashSet<String> so i tried to convert it to a json thinking that an array will be the output, below is my code.
var items = dataColumn[index]['items'];
final item = json.decode(json.encode(items.toString()));
When i print(items.runtimeType) the items is equals to _CompactLinkedHashSet<String>
but manage to convert it to a string using json encode and decode but the output is not an array its a string formatted as below
{'Word-1', 'Word-2', 'Word-3'}
I would like to convert it to an array so i can display it inside a ListView.builder like below
['Word-1', 'Word-2', 'Word-3']
You can change like below.
void main() {
var item = {'Word-1', 'Word-2', 'Word-3'};
print(item.runtimeType);
print(item);
// var itemList = item.map((item) => item).toList();
var itemList = List.from(item);
print(itemList.runtimeType);
print(itemList);
}

Why fromJson() function of Protobuf not working in Dart

I have an issue with the fromJson() function.
I try to build my protobuf with the data received from Firestore and the fromJson() seems to not be able to parse it.
Facing this issue, I decide to do a test by creating a new empty Protobuf manually, export it as a JSON and create a new protobuf with the json. I got some weird issues:
MyProtobuf my_protobuf = MyProtobuf();
my_protobuf.id = "ABC";
...
// Exporting using writeToJson()
String json1 = my_protobuf.writeToJson(); // All my keys are numbers.. why?
// Exporting using a Map
Map<String, dynamic> json2_map = info_to_write.toProto3Json();
String json2 = JsonEncoder().convert(json2_map); // Seems to be a normal JSON
// Build a protobuf from JSON
MyProtobuf new_protobuf1 = MyProtobuf.fromJson(json1); // Exception thrown
MyProtobuf new_protobuf2 = MyProtobuf.fromJson(json2); // Exception thrown
Is this a bug of Im not using this good function?
This is my proto file:
syntax = "proto3";
package test.v1;
message MyProtobuf {
string id = 1;
string name = 2;
}
To convert a message to json string, you need to use the following code:
MyProtobuf my_protobuf = MyProtobuf();
my_protobuf.id = "ABC";
...
// convert to json object
var obj = my_protobuf.toProto3Json();
// encode to json string
var value = jsonEncode(obj);
To decode json string to protobuf message, use the following:
var jsonString = ".....";
// decode to json object
var obj = jsonDecode(docJson);
var my_protobuf = MyProtobuf.create()..mergeFromProto3Json(obj);
see https://github.com/google/protobuf.dart/issues/220#issuecomment-863250957

COS How to set Ens.StringContainer to String Value

I like to understand how you can set Ens.StreamConainer to a string value. I just see a class for setting the OriginalFilename but nothing for setting the body.
s pRequest = ##class(Ens.StreamContainer).%New()
s pRequest.OriginalFilename = "Test"
d pRequest.Stream.Read(hl7) //Error Out
d pRequest.StreamSet(hl7) //Getting empty string
If hl7 is a stream:
s pRequest = ##class(Ens.StreamContainer).%New(hl7)
It hl7 is a string:
s pStream = ##class(%Stream.GlobalCharacter).%New()
do pStream.Write(hl7)
s pRequest = ##class(Ens.StreamContainer).%New(pStream)
Some code advice:
d pRequest.Stream.Read(hl7) //Error Out
Read reads from stream, and Write writes to stream.
d pRequest.StreamSet(hl7) //Getting empty string
It is a setter method for Stream property. There's no need to call it directly, just set the property.

Base64Encoding result string with "/" in Swift

I want to convert a UIImage to a string representation. I am using the following code:
let imageData = UIImagePNGRepresentation(resizedImage)
if let imageBase64 = imageData?.base64EncodedDataWithOptions(NSDataBase64EncodingOptions (rawValue: 0)) {
let strBase64:String = imageBase64.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
}
The resulting string looks something like:
aVZCT1J3MEtHZ29BQUFBTlNVaEVVZ0FBQWZRQUFBRk5DQUlBQUFCNWNRcGdBQUFBQVhOU1IwSUFyczRjNlFBQUFCeHBSRTlVQUFBQUFnQUFBQUFBQUFDbkFBQUFLQUFBQUtjQUFBQ21BQUg4T0dIYnkwY0FBRUFBU1VSQlZIZ0J0TDMxbHh4SHR1L3JQK25kYzk5ZDU3NHpaK0I0NXN5Y0FkUFlIbnRNR3RzeWcyekpZdWdXWTNjTEd0UnFadTVxWm1abVptWUd0Vmp2RzdFemQwVmxabFZYeXpOZTN4VnJaMlJXU2ZybFUxOS9ZMGZrUzdQTEQrZFdIczZ2UGxwWWZieTQ5bVJwL2NueStxT1ZqY2ZRNnVZVGFHM3JxZFRqOWUwbkxyUzI5ZGdnRncrdmJ6LzdtZHE0LzN4bDQxRjljOXZBK0p5enI5TC81dlQzZHpyU3Y1SCtzU3Y0OTI0OFc5dDRqa0xxMmNxbVhjc2J6d3hhV245cTBOemEwL24xWnhnTm1sMTk0a3d6cTQ5VlRhODhZVTB0UDJ
But the format I am looking for should contain "/", like the following (a random image I found):
/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxMTEhUSEhMWFRUWGBcZGRgXFxcVFRgYFxUWFhcVFxgYHSggGBolGxcXITEhJSkrLi4uFx8zODMtNygtLisBCgoKDg0OGhAQGisdHx0tLS0tKy0tLS0tLSstLS0tLS0tLS0tKy0tLS0tLSstLS0tLSs3Ky0tKysrLSstKysrK//AABEIAMwAzAMBIgACEQEDEQH/xAAcAAABBAMBAAAAAAAAAAAAAAAGAgMEBQABBwj/xABCEAABAwIDBQUGAwYFBAMBAAABAgMRAAQSITEFBkFRYRMicYGRBzJCobHRFCPBUmKCk+HwM0NTctIVkqLxRGOyJP/
I don't know how to get the second format (containing the "/").
You're encoding it twice. Just do
let strBase64 = imageData?.base64EncodedStringWithOptions([])

Read data with ILNumerics.IO.HDF5

I try ILNumerics.IO.HDF5 and can not read the following data:
Variable length strings in Datasets and Attributes.
Datasets with variable length arrays. Each cell contain a array of numbers, which are histograms.
Compound data, ie. Datasets with structs containing some numbers.
In HDFView 2.10.1 I can read this data:
https://anonfiles.com/file/13756916026cafc4e4ec7c333f235bda
How can I use ILNumerics.IO.HDF5 with this data?
I found an other post with suggestion to read string as char.
But with the variable length string an exception is thrown: "Error reading data from the attribute!"
var file = new H5File("test.h5");
H5Dataset ds1 = file.First<H5Dataset>("Wind");
var att = ds1.Attributes["Aggregator"];
var value = att.Get<char>();
Could you provide more info on how you write the string attributes and what exactly is the issue. When you say 'can not read',Do you get a null return value or do you get an exception.
I write strings as attributes in my application and it works fine. I am guessing there could be a problem in the way you write the string. As per Haymo's suggestion, I convert the string into char array and write as attribute. Here is the sample code
private ILRetArray<Char> ConvertStringToArray(string str)
{
using (ILScope.Enter())
{
ILArray<Char> A = ILMath.array<Char>(' ', 1, str.Length);
for (int i = 0; i < str.Length; i++)
{
A.SetValue(str[i], 0, i);
}
return A;
}
}
Test Case :
using (var file = new H5File("testwrite.h5"))
{
var ds = new H5Dataset("data", ILMath.rand(10,10));
file.Add(ds);
string teststr = "Test string";
ILArray<char> charStr = ConvertStringToArray(mystr);
ds.Attributes.Add(new H5Attribute("mystring",charStr));
//Read back the dataset and its attributes
var group = file.Find<H5Dataset>("data").First();
ILArray<Char> storedData = group.Attributes["mystring"].Get<Char>();
}