0

Asking after searching and trying many examples . I'm trying to dynamically get list of values from json in order to insert into array .

The json looks like :

{
"Test_name":"mft",
"parameters":[ 
{
"Remotehost":"vl-tlv-ctm-qa22",
"Ftptype":"sftp",
"file_name":"blabla.txt"
}
]
}

i'm using the following code :

JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("c:/temp/test.txt"));
JSONObject jsonObject = (JSONObject) obj;
String testname = (String) jsonObject.get("Test_name");
System.out.println(testname)
JSONArray msg = (JSONArray) jsonObject.get("parameters");
Iterator<JSONObject> iterator = msg.iterator();
while (iterator.hasNext()) {
JSONObject factObj = (JSONObject) iterator.next();
System.out.println(factObj);

the output is :

mft
{"Remotehost":"vl-tlv-ctm-qa22","file_name":"blabla.txt","Ftptype":"sftp"}

how do i break the pairs in the nested so i can use the as variables and not as single line ?

Thanks , Zohar

1 Answer 1

2

You can get the pairs as key-value pair from JSONObject as below:

        while (iterator.hasNext()) {
            JSONObject factObj = (JSONObject) iterator.next();
            for (Object key : factObj.keySet()) {
                System.out.println(key+":"+factObj.get(key));
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.