How do I convert azure search string to List of search documents. is it possible to convert the below string using jackson. I know this is not a valid JSON.
The below string comes from Azure search and the type is Collection Edm ComplexType.
Can I able convert the below string using Jackson or simple java 8 streams ?
public class test {
public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
// TODO Auto-generated method stub
String str = "[{empId=abc, empName=name, empSal=100, empEmail=test@gmail.com}, {empId=xyz, empName=test,abcexample, empSal=200, empEmail=test12@gmail.com}]";
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
List<SearchDocument> arr = mapper.readValue(str, (new ArrayList<SearchDocument>()).getClass());
System.out.println(arr);
}
}
I tried the below approach, but a string has comma in value getting issue.
'String str = "[{empId=abc, empName=name, empSal=100, empEmail=test@gmail.com}, {empId=xyz, empName=test,abcexample, empSal=200, empEmail=test12@gmail.com}]";
String elementType = StringUtils.substringBetween(str, "[", "]");
String regex = "\\{|\\}|\\[|\\]";
elementType = elementType.replaceAll(regex, " ");
StringTokenizer token = new StringTokenizer(elementType, ",");
while(token.hasMoreElements()){
String str1 = token.nextToken();
StringTokenizer furtherToken = new StringTokenizer(str1,"=");
while(furtherToken.hasMoreTokens()){
System.out.println("Key = "
+ furtherToken.nextToken() + ", Value = " + furtherToken.nextToken());
}
}
'