2012-11-19 89 views
3

可能的复制如何将数据从服务器的JSON数组,并转换到Java数组中的Android应用程序

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" 
    ] 
] 

这使用的是数据我想要一个java数组(创建一个动态表来显示数据)。

我可以用得到的文本值这个

String response = null; 
      try { 
       response = CustomHttpClient.executeHttpPost("http://test.tester.com/check.php", postParameters); 
       String res=response.toString();} 
+0

你要提供方法的更多信息。你的服务器上运行什么软件?你对应用程序有控制吗?如果是这样,你可以控制它通过输出的方式发送给客户端,并从那里... – Ben

+0

我得到一个使用PHP脚本的JSON数组。我想让这个json数组到android应用程序并将其转换为java数组。如果你能帮上忙,那会很棒。谢谢。 –

+0

可能的重复:http://stackoverflow.com/questions/2255220/how-to-parse-a-json-and-turn-its-values-into-an-array –

回答

3

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

您例如:

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

,你将不得不这样做的这个效果的东西:

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

这将返回数组对象。

然后迭代将是如下:

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

你将不得不以确定是否该数据是一个数组(简单地检查 该charAt(0)开始于[字符)。

希望这会有所帮助。

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

1

第一解析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()); 

      } 

此JSON阵列和存储,然后使用for循环店字符串数组..

+0

你能解释一下JSONObject jobj = new JSONObject(signedData);部分 –

+0

signedData是你的json数据的字符串.. –

1

你的JSON转换为Java的主要取决于您正在使用哪个库来执行任务。这里的其他答案使用org.json库,但是大多数极客会因其使用情况而发生剧烈反应,因为它非常缓慢。我知道的速度最快的图书馆是杰克逊,但我个人更喜欢Google-GSON,因为它足够快,但仍然非常易于使用。

看着你的示例字符串,你似乎有一个字符串数组的数组。在Gson,你想把它们看作CollectionCollectionStrings。这里的示例代码:

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

和输出:

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, 

这是从谷歌,GSON用户指南采取:https://sites.google.com/site/gson/gson-user-guide#TOC-Collections-Examples

+0

谢谢Matt Quiros。 –

+0

如果您有任何帮助,您可以自由选择所选答案。 :) –

相关问题