0

I am getting response from JSON in two format String and Array, so am not able to handle it becuase i am not sure which format will be

Is there any way to handle string and array type response using java

Kindly let me know thanks in advance

Cloudravi
  • 63
  • 1
  • 11
  • What is the json library you use? Use a generic field getters(object type) like in jackson lib https://stackoverflow.com/questions/26190851/get-single-field-from-json-using-jackson – Whome Mar 04 '19 at 06:26
  • What do you mean by two formats? is it a string followed by an array (in which case it wouldn't be json?) is it a object with two fields? – Maurice Perry Mar 04 '19 at 06:29
  • REST Assured actually i am doing automation for api, but the problem i am facing is message variable which holds string reponse sometime and sometime array response, when i want to store as string i get error cannot cast string into array like that – Cloudravi Mar 04 '19 at 06:29
  • Can you give some more information about your JSON response? – Krishna Vyas Mar 04 '19 at 06:31
  • Ex1 : { "status_code": 2002, "sub_status_code": 2002, "message": "Service Invoke Create Step2 Success", } Example 2 : { "status_code": 7000, "sub_status_code": 7001, "message": [ "Step No: Step 2: Line No: 92 Variable Name: lan-pool-ipv6-mask Error Code: 7516 Error Detail: Lan Pool IPV6 network address is invalid Description: " ], in example 1 message is string type and in example 2 message is array type how to handle it in java – Cloudravi Mar 04 '19 at 06:38
  • which library you are using/? Please post your code. – Ninad Pingale Mar 04 '19 at 07:00

2 Answers2

0

I think you can use try catch exception in this case. First, try to treat the response as String. if it occurs error, just catch error and treat the response as Array.

Thanh Nham
  • 71
  • 6
0

Just check the datatype of JSON's message part and accordingly use that.

Check out this example.

package test;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class jsonDeco {

    public static void main(String[] args) throws JSONException {
        // TODO Auto-generated method stub
        JSONObject a = new JSONObject("{ \"status_code\": 2002, \"sub_status_code\": 2002,"
                + " \"message\": \"Service Invoke Create Step2 Success\"} ");
        JSONObject b = new JSONObject("{ \"status_code\": 7000, \"sub_status_code\": 7001,"
                + " \"message\": [ \"Step No: Step 2: Line No: 92 Variable Name: lan-pool-ipv6-mask"
                + " Error Code: 7516 Error Detail: Lan Pool IPV6 network address is invalid Description: \" ]}");
        parseAORB(b);
    }

    public static void parseAORB(JSONObject input) throws JSONException
    {
        String code = input.getString("status_code");
        System.out.println(code);
        System.out.println(nameOf(input.get("message")));
        if(nameOf(input.get("message")).equals("String"))
        {
            System.out.println("inside string");
            String message = input.getString("message");
            System.out.println("Message is : "+message);
        }
        else if (nameOf(input.get("message")).equals("JSONArray"))
        {
            JSONArray message = input.getJSONArray("message");
            System.out.println("inside jsonarray");
            System.out.println("Message is : "+message.getString(0).toString());
        }   
    }

    private static String nameOf(Object o) {
        return o.getClass().getSimpleName();
    }
}

You can pass variable a or b to test.

P.S. In the comment, both the example is wrong. In first one, there is one comma , extra at the end and in the second example, you didn't close the curly bracket }.

driftking9987
  • 1,673
  • 1
  • 32
  • 63