Parsing A Json File In Android Application
Solution 1:
The d:"[ ... ]"
is not an JSONArray
but a String
.
You need to get that string and parse to JSON again.
Solution 2:
Why not make life simpler for yourself and use Gson? Based on your code I knocked up a quick sample:
Create a POJO to represent your model:
publicclassResult{
@SerializedName("e")public String e;
@SerializedName("c")public int c;
@SerializedName("u")public String u;
@SerializedName("p")public boolean p;
@SerializedName("d")public String[] d;
}
Now create an instance from the Json string
String json = "//some json string//";
Gson gson = new Gson();
Result result = gson.fromJson(json, Result.class);
//do something with resultif(result.p)
{
//argh we have a "p"
}
//or maybe we iterate over the strings you were afterfor(String myString : result.d)
{
//do something with myString which might be set to "evil" etc
}
Solution 3:
I think you have problem in that string:
String[] finals = null;
You don't create array of Strings but use it in cycle:
finals[i] = content;
In that case i can reccomend you to use ArrayList
:
List<String> finals = newArrayList<String>();
...
finals.add(content);
And your method can return List<String>
, but if you need Array, you can transform it:
String[] array = newString[finals.size()];
finals.toArray(array);
returnarray;
Solution 4:
EDIT2
I almost guessed - NetworkOnMainThreadException
. Move the code on different thread. I recommend using AsyncTask
EDIT
if the app crashes before the try block, then most probably you have missed to add this permission to your manifest:
<uses-permissionandroid:name="android.permission.INTERNET" />
also, you need to change this line
content.substring(content.indexOf("\\u003Cb\\u003E ")+"\\u003Cb\\u003E ".length(), content.indexOf("\\u003C\\/b"));
to this
content = content.substring(content.indexOf("\\u003Cb\\u003E ")+"\\u003Cb\\u003E ".length(), content.indexOf("\\u003C\\/b"));
the method does not affect the current instance of the string but returns a new one.
Post a Comment for "Parsing A Json File In Android Application"