简体   繁体   English

无法解析Android的JSON文件

[英]Trouble parsing JSON files for Android

I am currently working on the Android front end for an open source project called Opinionated. 我目前正在为名为Opinionated的开源项目开发Android前端。 This is my first android development experience. 这是我的第一个android开发经验。 The app is a news aggregator that links users to similar articles as the one they are reading. 该应用程序是一个新闻聚合器,可将用户链接到他们正在阅读的类似文章。 The back-end produces JSON files which I need to parse to create buttons/textviews etc... 后端产生JSON文件,我需要对其进行分析以创建按钮/文本视图等。

I have JSON files stored in the assets folder of my android project and I have a function that returns the contents of the JSON file as a string. 我将JSON文件存储在我的android项目的资产文件夹中,并且具有一个将JSON文件的内容作为字符串返回的函数。 I use this string to create a JSONObject, but when I try to use this string to create a JSONArray an exception is thrown. 我使用此字符串创建JSONObject,但是当我尝试使用此字符串创建JSONArray时,将引发异常。

My JSON file looks like this: 我的JSON文件如下所示:

{
"article":{
        "title"         : "Justices Hearing Arguments on ‘One Person, One Vote’",
        "description"   : "The Supreme Court will address a voting rights case that has the potential to shift political power from urban areas to rural ones. The Supreme Court has never definitively ruled on who must be counted in voting districts: all residents or just eligible voters?",
        "author"        : "ADAM LIPTAK",
        "date"          : "2015-12-08",
        "link"          : "http://www.nytimes.com/2015/12/09/us/politics/supreme-court-to-hear-arguments-on-one-person-one-vote.html?hp&action=click&pgtype=Homepage&clickSource=story-heading&module=first-column-region&region=top-news&WT.nav=top-news",
        "image"         : "http://static01.nyt.com/images/2015/12/08/us/09scotus-web/09scotus-web-master675.jpg",
        "imageCaption"  : "Voters in Columbus, Ohio, last month",
        "body"          : "WASHINGTON — A closely divided Supreme Court on Tuesday struggled to decide “what kind of democracy people want,” as Justice Stephen G. Breyer put it during an argument over the meaning of the constitutional principle of “one person one vote.” The case basic question in the case, Evenwel v. Abbott, No. 14-940, is who must be counted in drawing voting districts: all residents or just eligible voters? The difference matters, because people who are not eligible to vote — children, immigrants here legally who are not citizens, unauthorized immigrants, people disenfranchised for committing felonies, prisoners — are not spread evenly across the country. With the exception of prisoners, they tend to be concentrated in urban areas. Their presence amplifies the voting power of people eligible to vote in urban areas, usually helping Democrats. Rural areas that lean Republican, by contrast, usually have higher percentages of residents eligible to vote. Continue reading the main story. RELATED COVERAGE Q. and A.: Examining a Voting Rights CaseDEC. 8, 2015 The John J. Moran Medium Security Prison in Cranston, R.I. A lawsuit filed in that state by the American Civil Liberties Union objects to counting prisoners when drawing voting districts.Sidebar: A.C.L.U.’s Own Arguments May Work Against It in Voting Rights CaseOCT. 12, 2015 Voting booths in Houston. Two Texas plaintiffs are challenging State Senate districts apportioned by the number of residents rather than eligible voters, saying it dilutes their voting power.Supreme Court Agrees to Settle Meaning of ‘One Person One Vote’MAY 26, 2015 The court’s decision in the case, expected by June, has the potential to shift political power from urban areas to rural ones, a move that would provide a big boost to Republican voters in many parts of the nation. Chief Justice John G. Roberts Jr. seemed attracted to counting only voters. “It is called ‘one person one vote,’” he said. “That does seem designed to protect voters.” But Justice Sonia Sotomayor said there were other interests at stake. “There’s a voting interest,” she said, “but there is also a representational interest.” Continue reading the main story RELATED IN OPINION Contributing Op-Ed Writer: The Supreme Court’s Identity Crisis on Voting RightsMARCH 19, 2015 Justice Anthony M. Kennedy seemed to be looking for a middle ground. “You should at least give some consideration to the disparity you have among voters” in different voting districts, he said. The “one person one vote” principle, rooted in cases from the 1960s that revolutionized democratic representation in the United States, applies to the entire American political system aside from the Senate, where voters from states with small populations have vastly more voting power than those with large ones. Everywhere else, voting districts must have very close to the same populations. But the Supreme Court has never definitively ruled on who must be counted. Tuesday’s case, a challenge to voting districts for the Texas Senate, was brought by Sue Evenwel and Edward Pfenninger. They are represented by the Project on Fair Representation, a small conservative advocacy group that has been active in cases concerning race and voting. The group was on the winning side in 2013 in Shelby County v. Holder, which effectively struck down the heart of the Voting Rights Act, freeing nine states, mostly in the South, to change their election laws without advance federal approval. The group is also behind a challenge to affirmative action in admissions at the University of Texas at Austin to be argued on Wednesday. In court papers, Ms. Evenwel and Mr. Pfenninger said that they lived in “districts among the most overpopulated with eligible voters” and that “there are voters or potential voters in Texas whose Senate votes are worth approximately one and one-half times that of appellants.” Last year, a three-judge panel of the Federal District Court in Austin dismissed the case, saying that “the Supreme Court has generally used total population as the metric of comparison.” At the same time, the panel said, the Supreme Court has never required any particular standard. The choice, the panel said, belongs to the states. Continue reading the main story RECENT COMMENTS John Graubard 18 minutes ago At the time the Constitution was adopted only white property owning male citizens had the right to vote. Should we go back to that time? ... michjas 18 minutes ago Most of the non-voting population at issue here consists of the U.S.'s 75 million children. And of that total, 20 million are Hispanic,... raduray 18 minutes ago If we counted only voters, it would favor communities with smaller families (generally more liberal), as children don't vote. Plaintiffs,... SEE ALL COMMENTS  WRITE A COMMENT Almost all states and localities count everyone, and the Constitution requires “counting the whole number of persons in each state” for apportioning seats in the House of Representatives among the states. There are practical problems, many political scientists say, in finding reliable data to count only eligible voters. Federal appeals courts have uniformly ruled that counting everyone is permissible, and one court has indicated that it is required. In the process, though, several judges have acknowledged that the Supreme Court’s decisions provide support for both approaches. The federal appeals court in New Orleans said the issue “presents a close question,” partly because the Supreme Court had been “somewhat evasive in regard to which population must be equalized.” In 1990, Judge Alex Kozinski, in a partial dissent from a decision of the federal appeals court in San Francisco, said there were respectable arguments on both sides. CONTINUE READING THE MAIN STORY 341 COMMENTS Counting everyone, he said, ensures “representational equality,” with elected officials tending to the interests of the same number of people, whether they are voters or not. Counting only eligible voters, on the other hand, he said, vindicates the principle that voters “hold the ultimate political power in our democracy.” In 2001, the Supreme Court turned down an opportunity to decide the question, in another case from Texas. “The one-person-one-vote principle may, in the end, be of little consequence if we decide that each jurisdiction can choose its own measure of population,” Justice Clarence Thomas wrote. “But as long as we sustain the one-person-one-vote principle, we have an obligation to explain to states and localities what it actually means.”",
        "file"          : "exArt1.json",
        "template"      : "top",
        "similarArticles" : ["exArt4.json", "exArt5.json"]}}

My code that creates and uses the JSON object is as follows: 我创建和使用JSON对象的代码如下:

try {
        JSONObject main_obj = new JSONObject(loadJSONFromAsset());
        JSONArray jarray= main_obj.getJSONArray("article");
        JSONObject article = jarray.getJSONObject(0);
        String title=article.getString("title");
        Button button = new Button(this);
        button.setText(title);
        linearLayout.addView(button);

    } catch (JSONException e) {
        e.printStackTrace();
    }

Any help/pointers would be most appreciated, Thanks! 任何帮助/指针将不胜感激,谢谢!

Try this: 尝试这个:

try {
    JSONObject main_obj = new JSONObject(loadJSONFromAsset());
    JSONObject article= main_obj.getJSONObject("article");
    String title=article.getString("title");
    Button button = new Button(this);
    button.setText(title);
    linearLayout.addView(button);

} catch (JSONException e) {
    e.printStackTrace();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM