You need to sign in to do that
Don't have an account?
Arnas Baronas
I am new to development and I need help. Maybe someone could give me apex code example and explain me how to access
Data ---> Search ---> documents ---> produced-by = "RO"
Nested JSON parse using APEX
{ "data": { "xmlns": { "cpcdef": "ABCD", }, "search": { "total-result-count": "1", "ops:query": { "syntax": "CQL", "$": "123456" }, "range": { "begin": "1", "end": "1" }, "documents": [ "produced-by": "RO", }] } }Hello, Everybody !
I am new to development and I need help. Maybe someone could give me apex code example and explain me how to access
Data ---> Search ---> documents ---> produced-by = "RO"
Greetings to you!
- Please use the below code [Solved].
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
All Answers
Hi Arnas,
I found the bit for a glitch in your JSON format, but if it is something like this
there are two ways to do that
First Method: get the complete JSON in string form in apex variable and call the getProducedBy method some what like this
It will return the name of the produced by.
Second Method: If your JSON keys and structure is not subject to change then you can create a wrapper class and deserialize your JSON directly into it, but since the class names in apex cant contains colon and variable names cant contain hyphen, you would have to replace those characters first, with some other characters in initial JSON sring before deserializing. and the on getting the result you would have to replace them back (In case some value in the result got changed initially will get back to normal)
NOTE: but this way its prone to errors as the characters you choosed might get replaced it they exist already in the string.
I hope it helps.
Use bellow wrapper it may helpful for you
public class Wrapper{
public Data data;
public Search search;
}
public class Data{
public Xmlns xmlns;
}
public class Xmlns{
public String cpcdef;
}
public class Search{
public String total-result-count;
public Ops:query ops:query;
public Range range;
public Documents documents;
}
public class Ops:query{
public String syntax;
public String $;
}
public class Range{
public String begin;
public String end;
}
public class Documents{
public String produced-by;
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Greetings to you!
- Please use the below code [Solved].
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com