如何从JSON数据文件创建JSON数组? [英] How to create JSON array from JSON data file?
问题描述
是Android的新手.我正在创建一个解析JSON数据并将其显示在ListView中的android项目.我正在使用Eclipse创建项目.我在原始文件夹中创建了一个名为userinput.json的文件,该文件是res的子文件夹.我已经在文件中提供了这些JSON数据.
Am new to android. Am creating an android project which parses the JSON data and displays it in a ListView. Am using Eclipse to create the project. I have created a file called userinput.json in the raw folder which is subfolder of res. I have given these JSON data inside the file.
[
{"name":"Company A", "hometown":"NJ"},
{"name":"Company B", "hometown":"PA"},
{"name":"Company C", "hometown":"CT"},
{"name":"Company D", "hometown":"NY"},
{"name":"Company E", "hometown":"NJ"}
]
我的要求是将原始资源中声明的JSON数据转换为JSON数组. 由于我已经在文件中声明了json数据,因此我必须从资源中访问它,因此我使用BufferedReader来读取文件. 但是,当尝试将StringBuilder对象转换为JSON数组时,返回的值为null. 这是我编写的代码:
My requirement is to convert this JSON data declared in the Raw resources into JSON array. Since I have declared the json data in a file, I have to access it from the resources and hence I use BufferedReader to read the file. But while trying to convert the StringBuilder object to JSON array, I am returned with null. This is the code I have written:
BufferedReader jsonReader = new BufferedReader(new InputStreamReader(
this.getResources().openRawResource(R.raw.userinput)));
StringBuilder jsonBuilder = new StringBuilder();
try {
for (String line = null; (line = jsonReader.readLine()) != null;)
{
jsonBuilder.append(line).append("\n");
}
jTokener = new JSONTokener(jsonBuilder.toString());
jsonArray = new JSONArray(jTokener.toString());
}
catch (Exception e)
{
throw new RuntimeException(e);
}
ArrayList<User> newArrayOfUsers = User.JsonData(jsonArray);
在单独的Java文件中,我有以下两种方法将JSON数组转换为Java对象:
In a seperate Java file I have the below two methods to convert my JSON Array into Java Objects:
public User(JSONObject object)
{
try
{
this.name = object.getString(name);
this.hometown = object.getString(hometown);
}
catch(JSONException e)
{
e.printStackTrace();
}
}
public static ArrayList<User> JsonData(JSONArray jsonArray)
{
ArrayList<User> users = new ArrayList<User>();
for(int i = 0; i < jsonArray.length(); i++)
{
try
{
users.add(new User(jsonArray.getJSONObject(i)));
}
catch(JSONException e)
{
e.printStackTrace();
}
}
return users;
}
我不确定是否以正确的方式执行此操作,有人可以指导我如何从StringBuilder获取JSON数组吗?
I am not sure, whether am doing this in correct way, Can anyone please guide me how to get JSON array from the StringBuilder?
推荐答案
这有效...
BufferedReader jsonReader = new BufferedReader(new InputStreamReader(
this.getResources().openRawResource(R.raw.userinput)));
StringBuilder jsonBuilder = new StringBuilder();
try {
for (String line = null; (line = jsonReader.readLine()) != null;) {
jsonBuilder.append(line).append("\n");
}
Toast.makeText(this, jsonBuilder.toString(), 1).show();
//JSONTokener jTokener = new JSONTokener(jsonBuilder.toString());
JSONArray jsonArray = new JSONArray(jsonBuilder.toString());
for(int i=0;i<jsonArray.length();i++)
{
JSONObject tmpjsonobject=jsonArray.getJSONObject(i);
Toast.makeText(this, tmpjsonobject.getString("name")+"\n"+tmpjsonobject.getString("hometown"), 1).show();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
@Salvin是正确的...不需要JsonTokener .....
and @Salvin is right ...there is no need for JsonTokener .....
这篇关于如何从JSON数据文件创建JSON数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!