Java Android 将字符串转换为 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20685241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Converting String to JSON
提问by user3082271
I need advice into how to work out a method in Android which has to pick a string loaded with JSON data and then convert it back to JSON.
我需要关于如何在 Android 中制定一种方法的建议,该方法必须选择一个加载了 JSON 数据的字符串,然后将其转换回 JSON。
For the time being, I have programmed the following but I'm not sure if I'm on the right track or not.
目前,我已经编写了以下程序,但我不确定我是否在正确的轨道上。
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");
}
}
The JSON is loaded into a String in this other method:
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);
}
And, lastly, the JSON is created within the MainActivity.java, not as a split file yet that does work correctly:
最后,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();
In short: what I want to know is if my method convert_JSON is on the right track or I'm misunderstanding how it's supposed to work like.
简而言之:我想知道的是我的方法 convert_JSON 是否在正确的轨道上,或者我误解了它应该如何工作。
Thank you very much for your help.
非常感谢您的帮助。
Yours sincerely,
此致,
Mauro.
毛罗。
回答by Husman
回答by Entreco
You can convert a jsonstring back to json using the following JSONObject(String json)
constructor:
您可以使用以下JSONObject(String json)
构造函数将 jsonstring 转换回 json :
JSONObject jsonObject = new JSONObject(jsonString);
Just, put it inside a try
catch
block and you should be good to go :)
只是,把它放在一个try
catch
块里,你应该很高兴:)
回答by Phil
You can create a JSONObject
from a String
using the constructor:
您可以使用构造函数JSONObject
从 a创建一个:String
JSONObject json = new JSONObject(myString);
And to convert your JSONObject
to a String
, just use the toString()
method:
并将您的转换JSONObject
为 a String
,只需使用以下toString()
方法:
String myString = json.toString();
Additionally, if you are trying to get a specific String
value from the JSONObject
, you can do this:
此外,如果您尝试从 中获取特定String
值JSONObject
,您可以这样做:
if (json.has("content"))
{
String content = json.getString("content");
//do something with content string
}
Finally, if you aren't very comfortable using JSONObject
, I recommend using the tools provided by droidQueryto help you parse, such as:
最后,如果你不是很习惯使用JSONObject
,我推荐使用droidQuery提供的工具来帮助你解析,例如:
Object[] array = $.toArray(myJSONArray);
and
和
Map<String, ?> map = $.map(myJSONObject);