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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:35:26  来源:igfitidea点击:

Android Converting String to JSON

javaandroidjsonstring

提问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

If you want a pure copy/paste example have a look hereAlternatively I would suggest using one of the many well documented libraries. My personal favourite is GSON

如果您想要一个纯复制/粘贴示例,请查看此处或者,我建议使用许多记录良好的库之一。我个人最喜欢的是GSON

Plenty of examples on the net on how to use this.

网上有很多关于如何使用它的例子。

回答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 trycatchblock and you should be good to go :)

只是,把它放在一个trycatch块里,你应该很高兴:)

回答by Phil

You can create a JSONObjectfrom a Stringusing the constructor:

您可以使用构造函数JSONObject从 a创建一个:String

JSONObject json = new JSONObject(myString);

And to convert your JSONObjectto a String, just use the toString()method:

并将您的转换JSONObject为 a String,只需使用以下toString()方法:

String myString = json.toString();

Additionally, if you are trying to get a specific Stringvalue from the JSONObject, you can do this:

此外,如果您尝试从 中获取特定StringJSONObject,您可以这样做:

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);