1

I am not sure if I am doing some silly mistake, What I am trying to achieve is I have JSON list and I want to convert them into multiple objects depending on variable argument passed to function.

Unit u1= new Unit();
User us = new User();
//calling funtion
StaticUtil.MagicJsonMapper(list, u1,us);
System.out.println(u1.getUnitName()); //place -1 unitName is null after function call

Inside static class I have create a function

@SuppressWarnings("rawtypes")
public static void MagicJsonMapper(List list,Object... objects){
    if(list.size()!= objects.length){
        //TODO
        System.out.println("parame`ter mismatch");
        return;
    } 
    int i=0;
    ObjectMapper mapper = new ObjectMapper();
    for(Object object : objects){
        if(list.get(i) instanceof List){
            MagicJsonMapper((List)list.get(i),object);
        }
        else{
            objects[i] = mapper.convertValue(list.get(i), object.getClass());
        }
        i++;
    }
 //place -2 "objects" contains proper value of unitname 
}

The issue is I am still not getting proper value in parameter after finished running this method. It means argument values are not retained as in contrast of normal java behaviour, is it something to do with variable argument. Just for clarity I debugged the code and values are proper at the end of the function.

4
  • Not related, but you should respect Java convention and start your method name with a lowercase ;-) Commented Mar 22, 2015 at 10:28
  • "I am still not getting proper value in parameter after finished running this method" - can you show us how and where you try to access the value? Commented Mar 22, 2015 at 10:33
  • @Joffery : I should have taken care of that, Thanks :) Commented Mar 22, 2015 at 10:33
  • @Joffery: i have update place1 and place2 with comments Commented Mar 22, 2015 at 10:41

1 Answer 1

3

The objects array is created during the function call and discarded afterwards. If you need to access the values from the array after the call, you need to create the array explicitly.

Sign up to request clarification or add additional context in comments.

3 Comments

The problem would be this code is to ease my efforts to unwrap each json and typecast. if I am creating array and passing it, then later I have to unwrap it and typcast which increase my job. Do you think there can be any other solution to this problem.
@sanjaypatel your JSON contains various types to read, and you seem to know them and their order (since you pass objects of each class to your method). I believe you should probably consider creating a class where all these objects are fields, instead of using a generic list or array.
@Joffery: I creating a rest based application, my request to server can consist of n number of different probable combinations to different resource. I thought to make it more generic, with ur suggestion I will end creating many POJOs which I don't want as they might be used only once a while.

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.