-1

I have three strings. Following the value of String a;:

{"a":"value","b":"value"}

And the value of String b;:

[{"a":"value","b":"value"},{"c":"value","d":"value"}]

And the value of String c;:

[[{"a":"value","b":"value"},{"c":"value","d":"value"}],[{"e":"value2","f":"value"},{"g":"value2","h":"value"}]]

How to get the value of a of String a;, the value of a and c of String b; and a, d, f and h of String c; through JSON without use libraries beyond those already available?

Bob
  • 436
  • 5
  • 13
  • Use library like GSON https://sites.google.com/site/gson/gson-user-guide. – mayr Jul 23 '14 at 18:52
  • I want to do it without use libraries beyond those already available. – Bob Jul 23 '14 at 18:57
  • Take a look at: http://developer.android.com/reference/org/json/JSONObject.html – mayr Jul 23 '14 at 18:58
  • I've already read it, I asked this question because I did not understand much. – Bob Jul 23 '14 at 19:00
  • 1
    Try this to get value "a" from json string c: JSONObject jObj = new JSONObject(c); String a = jObj.getString("a"); – mayr Jul 23 '14 at 19:02
  • @mayr thanks you a lot, can you solve the mine other two examples in a answer please? – Bob Jul 23 '14 at 19:06

2 Answers2

1

Use JSONObject. For json String a:

JSONObject aObj = new JSONObject(a); 
String a = jObj.getString("a");
String b = jObj.getString("b");

Json string b:

JSONArray bArr = new JSONArray(b);
JSONObject first = bArr.getJSONObject(0);

Json string c:

JSONArray cArr = new JSONArray(b);
JSONArray innerArr = cArr.getJSONArray(0);
JSONObject first = innerArr.getJSONObject(0);
mayr
  • 451
  • 6
  • 14
0

Try using the GSON parser. See http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html or many other resources

Michael Levy
  • 13,097
  • 15
  • 66
  • 100
  • How to do it without using a library? – Bob Jul 23 '14 at 18:56
  • See http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java or http://www.tutorialspoint.com/json/json_java_example.htm or https://code.google.com/p/json-simple/wiki/DecodingExamples or many others. – Michael Levy Jul 29 '14 at 11:58