如何编辑/写回json文件?

时间:2015-10-30 03:44:22

标签: java json

下面是我从json文件中读取的java代码

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;



public class FileIO {

    @SuppressWarnings("unchecked")
    //public static Showtime [] movie = MoviesListing.movie();

    public void importTxt() {
        int j = 0;
        JSONParser parser = new JSONParser();

        try {
            JSONArray a = (JSONArray) parser.parse(new FileReader("C:/Users/Glambert/Dropbox/java/New folder/perfection/UPdate/json.txt"));

            for (Object o : a)
            {
                JSONObject person = (JSONObject) o;

                MoviesListing.movie[j].assign((String) person.get("moviename"));
                MoviesListing.movie[j].assignShowingStatus((String) person.get("showingstatus"));
                MoviesListing.movie[j].assignSynopsis((String) person.get("synopsis"));
                MoviesListing.movie[j].assignDirector((String) person.get("director"));
                MoviesListing.movie[j].assignCast((String) person.get("cast"));
                MoviesListing.movie[j].assignReviewerRate((double) person.get("reviewerRate"));
                MoviesListing.movie[j].assignPastReviews((String) person.get("pastreviews"));
                MoviesListing.movie[j].assignMovieRating((String) person.get("movierating"));
                MoviesListing.movie[j].assignShowtimes((JSONArray) person.get("showtimes"));

                j++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

它可以通过读取这样的json文件来实现:

[  
  {  
    "moviename":"Goosebumps",
    "showingstatus":"Now Showing",
    "synopsis":"Upset...",
    "director":"Rob Letterman",
    "cast":"Jack Black, Dylan Minnette, Odeya Rush, Amy Ryan",
    "reviewerRate": 3.2 ,
    "pastreviews":"Goosebumps...",
    "movierating":"PG",
    "showtimes":[  
        "09.30",
        "12.30"
    ],
    "job":"Teacher"
  },
  {  
    "moviename":"Bridge of Spies",
    "showingstatus":"Now Showing",
    "synopsis":"Tom Hanks stars..",
    "director":"Steven Spielberg",
    "cast":"Tom Hanks, Mark Rylance",
    "reviewerRate": 3.9,
    "pastreviews":"JBridge...",
    "movierating":"PG-13",
    "showtimes":[  
        "09.40",
        "10.45"
    ],
  }
]

但是,我想以相同或至少接近此格式的方式编写/编辑txt文件。谁知道怎么做?

1 个答案:

答案 0 :(得分:0)

在您提供的JSON格式中,删除最后一个数组后的逗号:

....
"showtimes":[  
    "09.40",
    "10.45"
    ]
  }
]

<强>解决方案: 你可以使用(从Maven下载Jar文件) http://mvnrepository.com/artifact/com.google.code.gson/gson

为您的数据创建一个Bean,如下所示:

MovieBean.java
private String moviename;
private String showingstatus;
...
private ArrayList<Double> showtimes;
private String job;

然后你可以使用, Gson方法fromJsontoJson分别用于创建到Bean和Bean到JSON的JSON。 您可以编辑Bean中的数据并将其再次保存在同一内容文件中。