第一件事是将这个 json 文件解析为 java,可以通过这种方式完成:-
try {
InputStream is;
//read the whole json file stored in assets
//below is android way of opening file from local resource folder, you can use other means to open
is = getApplicationContext().getAssets().open("jsonfile.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
//convert the json file to string
String bufferString = new String(buffer);
JSONObject jsonObject;
JSONArray jsonArray;
jsonObject = new JSONObject(bufferString);
jsonArray=jsonObject.getJSONArray("property");
for (int i=0;i<jsonArray.length();i++){
jsonObject = jsonArray.getJSONObject(i);
JSONObject s = jsonArray.optJSONObject(i);
String s2 = s.getString("value");
if(s2.contains("{")){
JSONObject jobject = new JSONObject(s2);
String valueUnderValue1 = jobject.getString("param_C_1");
String valueUnderValue2 = jobject.getString("param_C_2");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
然后创建一个类,该类将包含您从 json 文件中获得的所有值。假设该类是 MyClass,包含您从 json 文件中获得的所有值。
制作 MyClass 对象,然后
MyClass obj = new MyClass();
Gson gson = new Gson();
JSONObject onj = new JSONObject();
JSONArray userDataValues = new JSONArray();
//again convert to json
userDataValues.put(new JSONObject(gson.toJson(obj)));
//serialized the object
onj.put("property", userDataValues);
我希望这是你想要的。