24515

How to convert JSON formatted string of data rows of a table in to java array in android

Question:

I have a JSON string like this of data for a table in an android app. one of {} is a row of data for the table. I want to separate these {}s into an array and then each element inside this array into other sub-arrays separating other elements inside {}. Please suggest an appropriate way of accomplishing this criteria using JSON. Thank you.

[ { "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" } ]

Answer1:

ok so in that case the code to use is this

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

ok?

Answer2:

Use JSONObject for this <a href="http://developer.android.com/reference/org/json/JSONObject.html" rel="nofollow">http://developer.android.com/reference/org/json/JSONObject.html</a>

Example

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

Use the NodePOJO class to hold each row values.

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

Recommend