我有json文件我需要关于它的类文件
{
"CountryDetail":[
{
"C_country":"India",
"C_sunrise":1381107633,
"C_sunset":1381149604
},
"weatherDetail":
{
"w_id":711,
"w_main":"Smoke",
"w_description":"smoke",
}
,
"Tempdetail":
{
"t_temp":304.15,
"t_pressure":1009,
}
}
答案 0 :(得分:16)
尝试这个你将获得该文件的JSONObject。(Json应该是有效的)。 您可以通过clicking here
查看JsonJSONObject obj = new JSONObject(readJSONFromAsset());
public String readJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("yourFile.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
对于Kotlin ==>>
var obj = JSONObject(readJSONFromAsset())
fun readJSONFromAsset(): String? {
var json: String? = null
try {
val inputStream:InputStream = assets.open("yourFile.json")
json = inputStream.bufferedReader().use{it.readText()}
} catch (ex: Exception) {
ex.printStackTrace()
return null
}
return json
}
答案 1 :(得分:3)
如果您正在使用Kotlin Lnaguage(Android上较智能的Java版本),那么这应该可以回答您的问题。
代码段是
val jsonfile: String = applicationContext.assets.open("ela.json").bufferedReader().use {it.readText()}
jsonfile
是一个字符串。因此要将其转换/类型转换为JSONObject
。您可以执行以下操作。
val jsonObject = JSONObject(jsonfile)
所以很简单。它对我有用。
答案的来源来自此link。
答案 2 :(得分:1)
以干净的方式从资产中读取json对象。
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public JsonObject getJSONResource(Context context) {
try (InputStream is = context.getAssets().open("resource.json")) {
JsonParser parser = new JsonParser();
return parser.parse(new InputStreamReader(is)).getAsJsonObject();
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
return null;
}
之所以存在api inlcusion,是因为异常处理使用try with resources。目前我使用简单的json,考虑GSON。
答案 3 :(得分:1)
public String getAssetJsonData(Context context) {
String json = null;
try {
InputStream is = context.getAssets().open("myJson.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
Log.e("data", json);
return json;
}
In Activity:-
String data = getAssetJsonData(getApplicationContext());
Type type = new TypeToken<FrameJson>() {
}.getType();
FrameJson frameJson = new Gson().fromJson(data, type);
答案 4 :(得分:0)
Error: Parse error on line 7:
...}, "weatherDetail": { "w_id": 711,
----------------------^
Expecting 'EOF', '}', ',', ']', got ':'
如果您想验证您的json,请访问此站点 https://jsonlint.com/
答案 5 :(得分:0)
我这样做是为了将县名列表从json存储到本地数据库,这是我的代码:
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("countries.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray countries=obj.getJSONArray("countries");
for (int i=0;i<countries.length();i++){
JSONObject jsonObject=countries.getJSONObject(i);
String code=jsonObject.getString("code");
String nameAr=jsonObject.getString("nameAr");
String nameEn=jsonObject.getString("nameEn");
countryList.add(new Country(code,nameAr,nameEn));
}
DBHelper.getInstance(activity).insertCountries(countryList);
} catch (JSONException e) {
e.printStackTrace();
}
这是我的json格式:
{"countries": [
{
"nameEn": "Afghanistan",
"code": "AF",
"nameAr": "أفغانستان"
},
{
"nameEn": "Åland Islands",
"code": "AX",
"nameAr": "جزر أولان"
}]}