3

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.

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:

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:

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.

Thank you very much for your help.

Yours sincerely,

Mauro.

user3082271
  • 127
  • 1
  • 1
  • 12

3 Answers3

13

You can convert a jsonstring back to json using the following JSONObject(String json) constructor:

JSONObject jsonObject = new JSONObject(jsonString);

Just, put it inside a try catch block and you should be good to go :)

Entreco
  • 12,738
  • 8
  • 75
  • 95
6

You can create a JSONObject from a String using the constructor:

JSONObject json = new JSONObject(myString);

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

String myString = json.toString();

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

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 my droidQuery library to help you parse, such as:

Object[] array = $.toArray(myJSONArray);

and

Map<String, ?> map = $.map(myJSONObject);
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Phil
  • 35,852
  • 23
  • 123
  • 164
-1

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

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

Husman
  • 6,819
  • 9
  • 29
  • 47