0

So I'm looking to add a lot of users to a group messaging app that allows http post requests and I'm going to try to use the file upload function to read from a JSON but I'm a little confused as to how to write this in java:

{
  "members": [
    {
      "nickname": "Mom",
      "user_id": "1234567890",
      "guid": "GUID-1"
    },
    {
      "nickname": "Dad",
      "phone_number": "+1 2123001234",
      "guid": "GUID-2"
    },
    {
      "nickname": "Jane",
      "email": "[email protected]",
      "guid": "GUID-3"
    }
  ]
}

This is an exmaple of the JSON file that I need to write to, can someone explain how to write that in Java? (It would need nickname & phone_number fields, only those two per person) Thanks!

EDIT 1: Sorry, wasn't clear. I need to use Java to produce a file with these contents.

4
  • Is your question how to produce such a file? Commented Aug 19, 2015 at 16:29
  • you just need a small method that takes the nickname, and number as function parameters and then appends them to a JSON file in the correct format. just like you would write to a text file. Commented Aug 19, 2015 at 16:30
  • 1
    Are you trying to create such a json from Java or you want to model this in Java? Commented Aug 19, 2015 at 16:30
  • try github.com/google/gson Commented Aug 19, 2015 at 16:30

2 Answers 2

3

Try try https://github.com/google/gson

http://www.studytrails.com/java/json/java-google-json-parse-json-to-java.jsp

Example:

import com.google.gson.Gson;

public class JavaToJsonAndBack {

    public static void main(String[] args) {
        Albums albums = new Albums();
        albums.title = "Free Music Archive - Albums";
        albums.message = "";
        albums.total = "11259";
        albums.total_pages = 2252;
        albums.page = 1;
        albums.limit = "5";
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();
        System.out.println(gson.toJson(albums));

    }
}

This is how the resulting JSON looks like

{"title":"Free Music Archive - Albums","message":"","errors":[],
"total":"11259","total_pages":2252,"page":1,"limit":"5"}
Sign up to request clarification or add additional context in comments.

Comments

1

Treat {} as classes and [] as arrays:

import com.google.gson.annotations.SerializedName;

public class Message {
    @SerializedName("members")
    private List<Member> members;
...

public class Member {
    @SerializedName("nickname")
    private String nickname;
    @SerializedName("user_id")
    private String userId;
    @SerializedName("guid")
    private String guid;
...

To transform to JSON:

Message msg;
...
String jsonResult = new Gson().toJson(msg);

To get back from JSON:

Message msg = new Gson().fromJson(jsonStr, Message.class);

User guide: https://sites.google.com/site/gson/gson-user-guide

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.