0

I'm receiving data in the format:

"result": {"FirstData": {"One": 1, "Two": 2,...

First of all, what is this called usually in Java (2D array)?

Secondly, how do I loop over this to extract the strings?

I understand that if I only had "FirstData" in my array I can do:

public void onSuccess( String[] result)
{   
    for( String Name : result ) {
        System.out.println(Name);   
    }   

Going through the same logic for 2D arrays, it doesn't seem to print things out:

public void onSuccess( JSONObject result)
{   //Parse here

    }

EDIT:

Yes it's JSON and it looks like the code has gson (google JSON) installed

EDIT 2:

ABOVE CODE NOW CORRECTED

fiz
  • 906
  • 3
  • 14
  • 38

5 Answers5

2

JSON. Use a JSON parser to read the data. One popular parser for Java : google-gson

Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
1

You can try using Google Gson, https://code.google.com/p/google-gson/. It can take JSON strings and convert them to Java objects.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
Vijay
  • 59
  • 3
1

It looks kind of like JSON. JSON-strings is built like this:

Key: Values

However, Values can be new "key:values" so you can have:

"Key": ["InnerKey1":"Value1","Innerkey2","Value2"], "Key2": ["InnerKey1":"Value1","Innerkey2","Value2"] etc.

For you source:

  JSONObject myJSONObject = new JSONObject("Your resultstring goes here");
JSONObject resultObject = myJSONObject.getJSONObject("result");
JSONObject FirstDataObject = resultObject.getJSONObject("FirstData"); //Object with JSONObjects "One","Two" etc


//Since the part " {"One": 1, "Two": 2,..." in your string isn't an JSONArray you cannot do the following but if it were like this "["One": 1, "Two": 2,..." your could do this:

JSONArray FirstDataArray = resultObject.getJSONObject("FirstData"); //Array with JSONObjects "One","Two" etc

JSONArray arr = resultObject.getJSONArray("FirstData"); 
for (int i = 0; i < arr.length(); i++)
{
    String number = arr.getJSONObject(i).getString(0);
    ......
}
Joakim M
  • 1,793
  • 2
  • 14
  • 29
0

Use a JSON parser for Java and you can probably convert it into objects. You can find a variety of Java-based JSON parsers on http://www.json.org/

D.R.
  • 20,268
  • 21
  • 102
  • 205
0

This string is in the JSON format and hence it is recommended to use a JSON parser.

Read about JSON format in http://www.json.org/.

This link provides explains about JSON in java http://www.json.org/java/

anirudh
  • 4,116
  • 2
  • 20
  • 35