0

hi i am trying to add the class object with arrylists and need to display it in json format i used gson library for json format and iam tring to bind the java object into jsonformat has below

" {"type": "record","name": "Doc","doc": "adoc","fields": [{"name": "id","type": "string"}, {"name": "user_friends_count","type": ["int", "null"]}]"

and these the following classes and main method

public class RootFields {
public String type;
public String name;
public String doc;

}

public class Fields {
public String name;
public String type;

}

and this is my main method

public class JavaToJson{


    public static void main(String[] args) {
        // TODO Auto-generated method stub      
RootFields root = new RootFields();
List<Fields> F2 = new ArrayList<Fields>();
Fields F1 = new Fields();
Fields Fields1 = new Fields();
Fields Fields2 = new Fields();
root.type = "record";
root.name = "Doc";
root.doc = "adoc";
F1.name = "id";
F1.type = "string";
Fields1.name = "user_friends_count";
Fields1.type = "int";
Fields2.name = "user_location";
Fields2.type = "int";
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();

root.F2.add(F1);
System.out.println(gson.toJson(root));

    }

}

it shows me compile error F2 cannot be resolved or is not a field at F2 in "root.F2.add(F1)" where am i going wrong please help out thanks in advance.

1
  • Yeah root is a RootFields it does not have a F2 field Commented Jan 26, 2017 at 7:50

2 Answers 2

1

You have not declared F2 as a field in RootFields. Though from what I understand, you should use either a List or an array, not several fields for each instance of Fields.

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

Comments

0
class RootFields {
    public String type;
    public String name;
    public String doc;
    List <Fields> filelds = new ArrayList<>();
}

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.