java - How to pull individual pieces of data from a JSON output String? -


In Java, how do I drag data to just the "data" section in the following JSON output?

{"getuserhashrate": {"data": 1425, "runtime": 8.2659721374512, "version": "1.0.0"}}

Json.get ("data") does not work and returns the following error:

Exception to "main" org.json.JSONException thread: JSONObject ["data"] was not found. At org.json.JSONObject.get (JSONObject.java:454) so4308554.JsonReader.main (JsonReader.java:40)

You have a JSON object that has exactly one field: getuserhashrate

  {" getuserhashrate ": ...}  

There is another JSON object in that field that has its own field ("data", "runtime", etc.).

  {"data": 1425, "runtime": 8.2659721374512, ..}  

By looking at your stack trace, you're using the original Are (or).

You parse JSON and get the top-level object back through:

  JSONObject Root = New JSONObject (myJsonString);  

Then you can find the object present in the getuserhashrate field:

  JSONObject data = root.getJSONObject ("getuserhashrate" );  

Now you can access the fields of that object.


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -