How to get data as an json array from a server and convert it in to java array to use in android application

匿名 (未验证) 提交于 2019-12-03 08:56:10

问题:

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

回答1:

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



回答2:

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..



回答3:

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



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!