4

Possible duplicate

How to parse a JSON and turn its values into an Array?

[
   [
      "sn1",
      "Liquid_level",
      "85"
   ],
   [
      "sn2",
      "Liquid_level,Temperature",
      "95"
   ],
   [
      "sn2",
      "Liquid_level,Temperature",
      "50"
   ],
   [
      "sn3",
      "Liquid_level",
      "85.7"
   ],
   [
      "sn4",
      "Liquid_level",
      "90"
   ],
   [
      "sn5",
      "Volt_meter",
      "4.5"
   ],
   [
      "sn6",
      "Temperature",
      "56"
   ],
   [
      "sn8",
      "Liquid_level",
      "30"
   ]
]

This is the data i want to get to a java array (to create a dynamic table to show data).

i can get a text value using this

String response = null;
            try {
                response = CustomHttpClient.executeHttpPost("http://test.tester.com/check.php", postParameters);
                String res=response.toString();}
5
  • You've got to provide way more information. What software is running on your server? Do you have control over the application? If so you can control what it sends to clients by way of output and go from there… Commented Nov 19, 2012 at 10:00
  • I'm getting a json array using a php script. i want to get this json array to the android application and convert it to a java array. If you can help that would be great. Thank you. Commented Nov 19, 2012 at 10:06
  • Possible duplicate of: stackoverflow.com/questions/2255220/… Commented Nov 19, 2012 at 10:19
  • I'm writing you an answer using the GSON library, hold a sec... Commented Nov 19, 2012 at 10:29
  • I found the exact solution i need from this link androidhive.info/2012/01/android-json-parsing-tutorial Thank you very much Mr. Ravi Tamada. Commented Nov 20, 2012 at 4:45

3 Answers 3

4

As from How to parse a JSON and turn its values into an Array?:

for your example:

{'profiles': [{'name':'john', 'age': 44}, {'name':'Alex','age':11}]}

you will have to do something of this effect:

JSONObject myjson = new JSONObject(the_json);
JSONArray the_json_array = myjson.getJSONArray("profiles");

this returns the array object.

Then iterating will be as follows:

    int size = the_json_array.length();
    ArrayList<JSONObject> arrays = new ArrayList<JSONObject>();
    for (int i = 0; i < size; i++) {
      JSONObject another_json_object = the_json_array.getJSONObject(i);
            //Blah blah blah...
            arrays.add(another_json_object);
    }

//Finally
JSONObject[] jsons = new JSONObject[arrays.size()];
arrays.toArray(jsons);

//The end...

You will have to determine if the data is an array (simply checking that charAt(0) starts with [ character).

Hope this helps.

Credits to https://stackoverflow.com/users/251173/the-elite-gentleman

Sign up to request clarification or add additional context in comments.

Comments

1

first parse this json array and store in ArrayList

              try 
             {
                      ArrayList<String> arl= new ArrayList<String>();
                      JSONObject jobj = new JSONObject(signedData);
                      JSONArray jroot = jobj.getJSONArray("xxxxx");

              for(int i=0;i<jroot.length();i++)
              {

                  JSONObject jElement = jroot.getJSONObject(i);

                  arl.add(jElement.getString("xxxxx"));


              }


          }

          catch (Exception e)
          {
               Log.v("log", "Error parsing data " + e.toString());

          }

and then using forloop store to string Array..

1 Comment

can you explain the JSONObject jobj = new JSONObject(signedData); part
1

Your conversion of JSON to Java largely depends on which library you are using to perform the task. The other answers here use the org.json library, but most geeks will react violently over its usage because it's quite slow. The fastest library I know of is Jackson, but I personally prefer Google-GSON because it's fast enough and yet remains very easy to use.

Looking at your sample string, you seem to have an array of arrays of strings. In Gson, you want to think of them as a Collection of a Collection of Strings. Here's the sample code:

import java.lang.reflect.Type;
import java.util.Collection;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


public class Main {
    public static void main(String[] args) {
        // your sample JSON string, converted to a java string
        String json = "[\n   [\n      \"sn1\",\n      \"Liquid_level\",\n      \"85\"\n   ],\n   [\n      \"sn2\",\n      \"Liquid_level,Temperature\",\n      \"95\"\n   ],\n   [\n      \"sn2\",\n      \"Liquid_level,Temperature\",\n      \"50\"\n   ],\n   [\n      \"sn3\",\n      \"Liquid_level\",\n      \"85.7\"\n   ],\n   [\n      \"sn4\",\n      \"Liquid_level\",\n      \"90\"\n   ],\n   [\n      \"sn5\",\n      \"Volt_meter\",\n      \"4.5\"\n   ],\n   [\n      \"sn6\",\n      \"Temperature\",\n      \"56\"\n   ],\n   [\n      \"sn8\",\n      \"Liquid_level\",\n      \"30\"\n   ]\n]";

        // instantiate a Gson object
        Gson gson = new Gson();

        // define the type of object you want to use it in Java, which is a collection of a collection of strings
        Type collectionType = new TypeToken<Collection<Collection<String>>>(){}.getType();

        // happiness starts here
        Collection<Collection<String>> stringArrays = gson.fromJson(json, collectionType);

        // simply print out everything
        for (Collection<String> collection : stringArrays) {
            for (String s : collection) {
                System.out.print(s + ", ");
            }
            System.out.println();
        }
    }
}

And the output:

sn1, Liquid_level, 85, 
sn2, Liquid_level,Temperature, 95, 
sn2, Liquid_level,Temperature, 50, 
sn3, Liquid_level, 85.7, 
sn4, Liquid_level, 90, 
sn5, Volt_meter, 4.5, 
sn6, Temperature, 56, 
sn8, Liquid_level, 30, 

This is taken from the Google-GSON user guide: https://sites.google.com/site/gson/gson-user-guide#TOC-Collections-Examples

1 Comment

You're free to pick a chosen answer if it did any help. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.