4

可能重复

如何解析 JSON 并将其值转换为数组?

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

3 回答 3

4

如何解析 JSON 并将其值转换为数组?

对于您的示例:

{'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

于 2012-11-19T10:11:13.403 回答
1

您将 JSON 转换为 Java 很大程度上取决于您用于执行任务的库。这里的其他答案使用该org.json库,但大多数极客会对它的使用做出激烈反应,因为它很慢。我所知道的最快的库是 Jackson,但我个人更喜欢 Google-GSON,因为它足够快而且非常易于使用。

查看您的示例字符串,您似乎有一个字符串数组。在 Gson 中,您想将它们视为 a Collectionof a Collectionof Strings。这是示例代码:

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, 

这取自 Google-GSON 用户指南:https ://sites.google.com/site/gson/gson-user-guide#TOC-Collections-Examples

于 2012-11-19T10:42:43.530 回答
1

首先解析这个json数组并存储在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());

          }

然后使用 forloop 存储到字符串数组..

于 2012-11-19T10:11:52.340 回答