我需要關於如何解決Android中的方法的建議,該方法必須選擇一個裝有JSON數據的字符串,然後將其轉換回JSON。Android將字符串轉換爲JSON
目前,我編程了以下內容,但我不確定我是否在正確的軌道上。
private void convert_JSON()
{
String json;
//funcions per a cridar el string amb JSON i convertir-lo de nou a JSON
JSONArray jsas = new JSONArray();
for (int i =0; i < jsas.length(); i++)
{
JSONObject message = jsas.getJSONObject(i);
String content = message.getString("content");
}
}
的JSON被加載到一個字符串在此另一種方法:
private void read_JSON(String json)
{
JSONObject jObject = new JSONObject(json);
JSONArray jso3 = new JSONArray (jObject.getString("Nombres_Hijos"));
String name = jso3.getString("Nombre");
System.out.println(name);
String surname = jso3.getString("Apellidos");
System.out.println(surname);
int date = jso3.getInt("Año_nacimiento");
System.out.println(date);
JSONArray jsa2 = jso3.getJSONArray ("Nombres_Hijos");
String names = jsa2.toString();
for (int i=0; i < jsa2.length(); i++)
{
System.out.println(jsa2.getString(i));
}
jso3.toString(json);
}
而且,最後,JSON是MainActivity.java中創建的,而不是一個分裂的文件還沒有,它正常工作:
private void create_JSON(String json)
{
JSONObject jso = new JSONObject();
try {
jso.put("Nombre","Miguel");
jso.put("Apellidos", "Garcia");
jso.put("Año_nacimiento", 1990);
JSONArray jsa = new JSONArray();
jsa.put("Blur");
jsa.put("Clur");
jso.put("Nombres_Hijos", jsa);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jso.toString();
總之:我想知道的是,如果我的方法convert_JSON是在正確的軌道上或我誤解它是如何應該像工作。
非常感謝您的幫助。
此致敬禮
毛羅。
你讀過嗎? http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android – brandonscript