1

我有一个像这样的数据的 JSON 字符串,用于 android 应用程序中的表。其中一个 {} 是表的一行数据。我想将这些 {} 分成一个数组,然后将此数组中的每个元素分成其他子数组,将 {} 中的其他元素分开。请建议使用 JSON 完成此标准的适当方法。谢谢你。

[
     {
        "nodeName":"prime_mtsc22@smpp3",
        "nodeId":"MTSC3",
        "tidPrefix":"4",
        "optStatus":"offline",
        "daStart":"1",
        "daEnd":"3",
        "description":"Description"
     },
     {
        "nodeName":"prime_mtsc22@smpp2",
        "nodeId":"MTSC58",
        "tidPrefix":"1",
        "optStatus":"blocked",
        "daStart":"5",
        "daEnd":"10",
        "description":"new description"
     },
     {
        "nodeName":"prime_mtsc22@smpp1",
        "nodeId":"MTSC1",
        "tidPrefix":"15",
        "optStatus":"online",
        "daStart":"12",
        "daEnd":"20",
        "description":"Description"
     },
     {
        "nodeName":"prime_mtsc22@smpp0",
        "nodeId":"MTSC15",
        "tidPrefix":"15",
        "optStatus":"offline",
        "daStart":"25",
        "daEnd":"30",
        "description":"Description"
     }
]
4

2 回答 2

2

好的,在这种情况下,要使用的代码是这样的

String jsonString = <your jsonString>;

// THIS IS NOT NEEDED ANYMORE 
//JSONObject json = new JSONObject(jsonString);

JSONArray topArray = null; 
try {
     // Getting your top array

     // THIS IS NOT NEEDED ANYMORE 
     //topArray = json.getJSONArray(jsonString);

     //use this instead
     topArray = new JSONArray(jsonString);

      // looping through All elements
      for(int i = 0; i < topArray.length(); i++){
      JSONObject c = topArray.getJSONObject(i);

      //list holding row data
      List<NodePOJO> nodeList = new ArrayList<NodePOJO>(); 

      // Storing each json item in variable
      String nodeName = c.getString("nodeName");
      String nodeID = c.getString("nodeID");

      NodePOJO pojo = new NodePOJO();
      pojo.setNodeName(nodeName);
      //add rest of the json data to NodePOJO class

      //the object to list
      nodeList.add(pojo);

    }
} catch (JSONException e) {
        e.printStackTrace();
 }

好的?

于 2012-10-02T10:10:36.130 回答
1

对这个http://developer.android.com/reference/org/json/JSONObject.html使用 JSONObject

例子

    String jsonString = <your jsonString>;

    JSONObject json = new JSONObject(jsonString);
    JSONObject topArray = ; 
    try {
         // Getting your top array
         topArray = json.getJSONArray(TAG_ARRAY_TOP);

          // looping through All elements
          for(int i = 0; i < topArray.length(); i++){
          JSONObject c = topArray.getJSONObject(i);

          //list holding row data
          List<NodePOJO> nodeList = new ArrayList<NodePOJO>(); 

          // Storing each json item in variable
          String nodeName = c.getString("nodeName");
          String nodeID = c.getString("nodeID");

          NodePOJO pojo = new NodePOJO();
          pojo.setNodeName(nodeName);
          //add rest of the json data to NodePOJO class

          //the object to list
          nodeList.add(pojo);

        }
    } catch (JSONException e) {
            e.printStackTrace();
     }

使用 NodePOJO 类来保存每一行的值。

 public class NodePOJO {
    private String nodeName;
    // do for rest of the json row data
    public void setNodeName(String nodeName) {
          this.nodeName = nodeName;
    }
    public String getNodeName() {
         return this.nodeName;
    }
 }
于 2012-10-02T08:38:55.807 回答