How to parse JSON Array (Not Json Object) in Android
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a trouble finding a way how to parse JSONArray.
It looks like this:
[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]
I know how to parse it if the JSON was written differently (In other words, if I had json object returned instead of an array of objects).
But it's all I have and have to go with it.
*EDIT: It is a valid json. I made an iPhone app using this json, now I need to do it for Android and cannot figure it out.
There are a lot of examples out there, but they are all JSONObject related. I need something for JSONArray.
Can somebody please give me some hint, or a tutorial or an example?
Much appreciated !
java android json gson arrays
add a comment |
I have a trouble finding a way how to parse JSONArray.
It looks like this:
[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]
I know how to parse it if the JSON was written differently (In other words, if I had json object returned instead of an array of objects).
But it's all I have and have to go with it.
*EDIT: It is a valid json. I made an iPhone app using this json, now I need to do it for Android and cannot figure it out.
There are a lot of examples out there, but they are all JSONObject related. I need something for JSONArray.
Can somebody please give me some hint, or a tutorial or an example?
Much appreciated !
java android json gson arrays
1
Try this to see if your json is valid json.parser.online.fr
– An-droid
Sep 24 '13 at 9:02
are you using any library for parsing json? Use gson library for json parsing.Much Helpfull.
– nilkash
Sep 24 '13 at 9:03
It is valid. I parsed it in an app i did for an iPhone And it's working. I just dont know how to do it in Android
– SteBra
Sep 24 '13 at 9:03
1
@nikash, yeah i noticed i could use gson. only problem is, all examples i found are for parsing a JSONObject not JSONArray
– SteBra
Sep 24 '13 at 9:04
Post your code that you have tried so far.It will be helpful to give a precise answer accordingly.
– Spring Breaker
Sep 24 '13 at 9:09
add a comment |
I have a trouble finding a way how to parse JSONArray.
It looks like this:
[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]
I know how to parse it if the JSON was written differently (In other words, if I had json object returned instead of an array of objects).
But it's all I have and have to go with it.
*EDIT: It is a valid json. I made an iPhone app using this json, now I need to do it for Android and cannot figure it out.
There are a lot of examples out there, but they are all JSONObject related. I need something for JSONArray.
Can somebody please give me some hint, or a tutorial or an example?
Much appreciated !
java android json gson arrays
I have a trouble finding a way how to parse JSONArray.
It looks like this:
[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]
I know how to parse it if the JSON was written differently (In other words, if I had json object returned instead of an array of objects).
But it's all I have and have to go with it.
*EDIT: It is a valid json. I made an iPhone app using this json, now I need to do it for Android and cannot figure it out.
There are a lot of examples out there, but they are all JSONObject related. I need something for JSONArray.
Can somebody please give me some hint, or a tutorial or an example?
Much appreciated !
java android json gson arrays
java android json gson arrays
edited Jan 10 '14 at 10:44
SteBra
asked Sep 24 '13 at 9:01
SteBraSteBra
2,29242959
2,29242959
1
Try this to see if your json is valid json.parser.online.fr
– An-droid
Sep 24 '13 at 9:02
are you using any library for parsing json? Use gson library for json parsing.Much Helpfull.
– nilkash
Sep 24 '13 at 9:03
It is valid. I parsed it in an app i did for an iPhone And it's working. I just dont know how to do it in Android
– SteBra
Sep 24 '13 at 9:03
1
@nikash, yeah i noticed i could use gson. only problem is, all examples i found are for parsing a JSONObject not JSONArray
– SteBra
Sep 24 '13 at 9:04
Post your code that you have tried so far.It will be helpful to give a precise answer accordingly.
– Spring Breaker
Sep 24 '13 at 9:09
add a comment |
1
Try this to see if your json is valid json.parser.online.fr
– An-droid
Sep 24 '13 at 9:02
are you using any library for parsing json? Use gson library for json parsing.Much Helpfull.
– nilkash
Sep 24 '13 at 9:03
It is valid. I parsed it in an app i did for an iPhone And it's working. I just dont know how to do it in Android
– SteBra
Sep 24 '13 at 9:03
1
@nikash, yeah i noticed i could use gson. only problem is, all examples i found are for parsing a JSONObject not JSONArray
– SteBra
Sep 24 '13 at 9:04
Post your code that you have tried so far.It will be helpful to give a precise answer accordingly.
– Spring Breaker
Sep 24 '13 at 9:09
1
1
Try this to see if your json is valid json.parser.online.fr
– An-droid
Sep 24 '13 at 9:02
Try this to see if your json is valid json.parser.online.fr
– An-droid
Sep 24 '13 at 9:02
are you using any library for parsing json? Use gson library for json parsing.Much Helpfull.
– nilkash
Sep 24 '13 at 9:03
are you using any library for parsing json? Use gson library for json parsing.Much Helpfull.
– nilkash
Sep 24 '13 at 9:03
It is valid. I parsed it in an app i did for an iPhone And it's working. I just dont know how to do it in Android
– SteBra
Sep 24 '13 at 9:03
It is valid. I parsed it in an app i did for an iPhone And it's working. I just dont know how to do it in Android
– SteBra
Sep 24 '13 at 9:03
1
1
@nikash, yeah i noticed i could use gson. only problem is, all examples i found are for parsing a JSONObject not JSONArray
– SteBra
Sep 24 '13 at 9:04
@nikash, yeah i noticed i could use gson. only problem is, all examples i found are for parsing a JSONObject not JSONArray
– SteBra
Sep 24 '13 at 9:04
Post your code that you have tried so far.It will be helpful to give a precise answer accordingly.
– Spring Breaker
Sep 24 '13 at 9:09
Post your code that you have tried so far.It will be helpful to give a precise answer accordingly.
– Spring Breaker
Sep 24 '13 at 9:09
add a comment |
11 Answers
11
active
oldest
votes
use the following snippet to parse the JsonArray.
JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String name = jsonobject.getString("name");
String url = jsonobject.getString("url");
}
Hope it helps.
just one question if jsonarray includes numerics like [2].[4] and that numerics inside includes again an array so if i want to retrieve from especially lets say the array nested in numeric [4] in java code i must write int i=4???
– BiggDawgg
Feb 17 '15 at 19:36
@BiggDawgg: Glad it helped you. Post your question in a separate thread with JSON data so that it would be easy for me to answer.
– Spring Breaker
Feb 18 '15 at 5:24
add a comment |
I'll just give a little Jackson example:
First create a data holder which has the fields from JSON string
// imports
// ...
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyDataHolder {
@JsonProperty("name")
public String mName;
@JsonProperty("url")
public String mUrl;
}
And parse list of MyDataHolders
String jsonString = // your json
ObjectMapper mapper = new ObjectMapper();
List<MyDataHolder> list = mapper.readValue(jsonString,
new TypeReference<ArrayList<MyDataHolder>>() {});
Using list items
String firstName = list.get(0).mName;
String secondName = list.get(1).mName;
Can you give me an example how would i get JSON as a string from url i have, please ?
– SteBra
Sep 24 '13 at 10:37
2
Weeell here's something that I made up: pastebin.com/w4thyPVK
– vilpe89
Sep 24 '13 at 11:24
1
TheDefaultHttpClient
s execute-method is a blocking call so it has to be done in a background task, just to make clear why it is in separate class.
– vilpe89
Sep 24 '13 at 11:39
1
have you imported OnHttpGetCompletedListener? Like thisimport your_package_name_here.HttpGetTask.OnHttpGetCompletedListener;
– vilpe89
Sep 24 '13 at 12:45
1
Yeap, that was the problem... Thanks for solving my issue :)
– SteBra
Sep 24 '13 at 12:59
|
show 2 more comments
public static void main(String args) throws JSONException {
String str = "[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]";
JSONArray jsonarray = new JSONArray(str);
for(int i=0; i<jsonarray.length(); i++){
JSONObject obj = jsonarray.getJSONObject(i);
String name = obj.getString("name");
String url = obj.getString("url");
System.out.println(name);
System.out.println(url);
}
}
Output:
name1
url1
name2
url2
add a comment |
Create a class to hold the objects.
public class Person{
private String name;
private String url;
//Get & Set methods for each field
}
Then deserialize as follows:
Gson gson = new Gson();
Person person = gson.fromJson(input, Person.class); //input is your String
Reference Article: http://blog.patrickbaumann.com/2011/11/gson-array-deserialization/
I asume this "input" would be URL to that json? or is it something else?
– SteBra
Sep 24 '13 at 9:09
@Stebra, Exactly, I included that as a comment in the code.
– Kevin Bowersox
Sep 24 '13 at 9:09
This is a very simple solution. It should be the accepted one in my view
– FabioC
Nov 18 '17 at 11:38
add a comment |
In this example there are several objects inside one json array. That is,
This is the json array: [{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]
This is one object: {"name":"name1","url":"url1"}
Assuming that you have got the result to a String variable called jSonResultString:
JSONArray arr = new JSONArray(jSonResultString);
//loop through each object
for (int i=0; i<arr.length(); i++){
JSONObject jsonProductObject = arr.getJSONObject(i);
String name = jsonProductObject.getString("name");
String url = jsonProductObject.getString("url");
}
did you try this?
– TharakaNirmana
Sep 24 '13 at 9:16
Im having a hard time getting my JSONstring from URL. Maybe you could help with that?
– SteBra
Sep 24 '13 at 10:49
Send me the URL
– TharakaNirmana
Sep 24 '13 at 11:00
add a comment |
@Stebra See this example. This may help you.
public class CustomerInfo
{
@SerializedName("customerid")
public String customerid;
@SerializedName("picture")
public String picture;
@SerializedName("location")
public String location;
public CustomerInfo()
{}
}
And when you get the result; parse like this
List<CustomerInfo> customers = null;
customers = (List<CustomerInfo>)gson.fromJson(result, new TypeToken<List<CustomerInfo>>() {}.getType());
1
Result variable is your JSONArray String
– nilkash
Sep 24 '13 at 9:10
Would result variable be the url i need to fetch json from or already fetched json ?
– SteBra
Sep 24 '13 at 9:22
1
result string is already fetched JSONArray string
– nilkash
Sep 24 '13 at 9:25
by far the easiest solution
– Danilo Salvio
Sep 8 '16 at 3:35
add a comment |
A few great suggestions are already mentioned.
Using GSON is really handy indeed, and to make life even easier you can try this website
It's called jsonschema2pojo and does exactly that:
You give it your json and it generates a java object that can paste in your project.
You can select GSON to annotate your variables, so extracting the object from your json gets even easier!
add a comment |
My case
Load From Server Example..
int jsonLength = Integer.parseInt(jsonObject.getString("number_of_messages"));
if (jsonLength != 1) {
for (int i = 0; i < jsonLength; i++) {
JSONArray jsonArray = new JSONArray(jsonObject.getString("messages"));
JSONObject resJson = (JSONObject) jsonArray.get(i);
//addItem(resJson.getString("message"), resJson.getString("name"), resJson.getString("created_at"));
}
Hope it help
p.s "jsonObject.getString("messages")" type is jsonArray; first exist answer is cannot be convert string to jsonArray; i'm fix it.
– bong jae choe
Nov 5 '15 at 5:10
add a comment |
Create a POJO Java Class for the objects in the list like so:
class NameUrlClass{
private String name;
private String url;
//Constructor
public NameUrlClass(String name,String url){
this.name = name;
this.url = url;
}
}
Now simply create a List of NameUrlClass and initialize it to an ArrayList like so:
List<NameUrlClass> obj = new ArrayList<NameUrlClass>;
You can use store the JSON array in this object
obj = JSONArray;//[{"name":"name1","url":"url1"}{"name":"name2","url":"url2"},...]
It's not clear whatobj = JSONArray;
is supposed to do. What isJSONArray
in this context? Where does it come from? What is the purpose of initializingobj
with anew ArrayList<NameUrlClass>
, when you replace it directly afterwards anyways?
– Max Vollmer
Oct 24 '18 at 11:38
add a comment |
URL url = new URL("your URL");
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
//setting the json string
String finalJson = buffer.toString();
//this is your string get the pattern from buffer.
JSONArray jsonarray = new JSONArray(finalJson);
add a comment |
Old post I know, but unless I've misunderstood the question, this should do the trick:
s = '[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]';
eval("array=" + s);
for (var i = 0; i < array.length; i++) {
for (var index in array[i]) {
alert(array[i][index]);
}
}
2
Wrong language.
– Sotirios Delimanolis
Jun 30 '16 at 17:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18977144%2fhow-to-parse-json-array-not-json-object-in-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
use the following snippet to parse the JsonArray.
JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String name = jsonobject.getString("name");
String url = jsonobject.getString("url");
}
Hope it helps.
just one question if jsonarray includes numerics like [2].[4] and that numerics inside includes again an array so if i want to retrieve from especially lets say the array nested in numeric [4] in java code i must write int i=4???
– BiggDawgg
Feb 17 '15 at 19:36
@BiggDawgg: Glad it helped you. Post your question in a separate thread with JSON data so that it would be easy for me to answer.
– Spring Breaker
Feb 18 '15 at 5:24
add a comment |
use the following snippet to parse the JsonArray.
JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String name = jsonobject.getString("name");
String url = jsonobject.getString("url");
}
Hope it helps.
just one question if jsonarray includes numerics like [2].[4] and that numerics inside includes again an array so if i want to retrieve from especially lets say the array nested in numeric [4] in java code i must write int i=4???
– BiggDawgg
Feb 17 '15 at 19:36
@BiggDawgg: Glad it helped you. Post your question in a separate thread with JSON data so that it would be easy for me to answer.
– Spring Breaker
Feb 18 '15 at 5:24
add a comment |
use the following snippet to parse the JsonArray.
JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String name = jsonobject.getString("name");
String url = jsonobject.getString("url");
}
Hope it helps.
use the following snippet to parse the JsonArray.
JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String name = jsonobject.getString("name");
String url = jsonobject.getString("url");
}
Hope it helps.
edited Mar 23 '16 at 11:54
cegprakash
1,2651639
1,2651639
answered Sep 24 '13 at 9:05
Spring BreakerSpring Breaker
6,84932455
6,84932455
just one question if jsonarray includes numerics like [2].[4] and that numerics inside includes again an array so if i want to retrieve from especially lets say the array nested in numeric [4] in java code i must write int i=4???
– BiggDawgg
Feb 17 '15 at 19:36
@BiggDawgg: Glad it helped you. Post your question in a separate thread with JSON data so that it would be easy for me to answer.
– Spring Breaker
Feb 18 '15 at 5:24
add a comment |
just one question if jsonarray includes numerics like [2].[4] and that numerics inside includes again an array so if i want to retrieve from especially lets say the array nested in numeric [4] in java code i must write int i=4???
– BiggDawgg
Feb 17 '15 at 19:36
@BiggDawgg: Glad it helped you. Post your question in a separate thread with JSON data so that it would be easy for me to answer.
– Spring Breaker
Feb 18 '15 at 5:24
just one question if jsonarray includes numerics like [2].[4] and that numerics inside includes again an array so if i want to retrieve from especially lets say the array nested in numeric [4] in java code i must write int i=4???
– BiggDawgg
Feb 17 '15 at 19:36
just one question if jsonarray includes numerics like [2].[4] and that numerics inside includes again an array so if i want to retrieve from especially lets say the array nested in numeric [4] in java code i must write int i=4???
– BiggDawgg
Feb 17 '15 at 19:36
@BiggDawgg: Glad it helped you. Post your question in a separate thread with JSON data so that it would be easy for me to answer.
– Spring Breaker
Feb 18 '15 at 5:24
@BiggDawgg: Glad it helped you. Post your question in a separate thread with JSON data so that it would be easy for me to answer.
– Spring Breaker
Feb 18 '15 at 5:24
add a comment |
I'll just give a little Jackson example:
First create a data holder which has the fields from JSON string
// imports
// ...
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyDataHolder {
@JsonProperty("name")
public String mName;
@JsonProperty("url")
public String mUrl;
}
And parse list of MyDataHolders
String jsonString = // your json
ObjectMapper mapper = new ObjectMapper();
List<MyDataHolder> list = mapper.readValue(jsonString,
new TypeReference<ArrayList<MyDataHolder>>() {});
Using list items
String firstName = list.get(0).mName;
String secondName = list.get(1).mName;
Can you give me an example how would i get JSON as a string from url i have, please ?
– SteBra
Sep 24 '13 at 10:37
2
Weeell here's something that I made up: pastebin.com/w4thyPVK
– vilpe89
Sep 24 '13 at 11:24
1
TheDefaultHttpClient
s execute-method is a blocking call so it has to be done in a background task, just to make clear why it is in separate class.
– vilpe89
Sep 24 '13 at 11:39
1
have you imported OnHttpGetCompletedListener? Like thisimport your_package_name_here.HttpGetTask.OnHttpGetCompletedListener;
– vilpe89
Sep 24 '13 at 12:45
1
Yeap, that was the problem... Thanks for solving my issue :)
– SteBra
Sep 24 '13 at 12:59
|
show 2 more comments
I'll just give a little Jackson example:
First create a data holder which has the fields from JSON string
// imports
// ...
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyDataHolder {
@JsonProperty("name")
public String mName;
@JsonProperty("url")
public String mUrl;
}
And parse list of MyDataHolders
String jsonString = // your json
ObjectMapper mapper = new ObjectMapper();
List<MyDataHolder> list = mapper.readValue(jsonString,
new TypeReference<ArrayList<MyDataHolder>>() {});
Using list items
String firstName = list.get(0).mName;
String secondName = list.get(1).mName;
Can you give me an example how would i get JSON as a string from url i have, please ?
– SteBra
Sep 24 '13 at 10:37
2
Weeell here's something that I made up: pastebin.com/w4thyPVK
– vilpe89
Sep 24 '13 at 11:24
1
TheDefaultHttpClient
s execute-method is a blocking call so it has to be done in a background task, just to make clear why it is in separate class.
– vilpe89
Sep 24 '13 at 11:39
1
have you imported OnHttpGetCompletedListener? Like thisimport your_package_name_here.HttpGetTask.OnHttpGetCompletedListener;
– vilpe89
Sep 24 '13 at 12:45
1
Yeap, that was the problem... Thanks for solving my issue :)
– SteBra
Sep 24 '13 at 12:59
|
show 2 more comments
I'll just give a little Jackson example:
First create a data holder which has the fields from JSON string
// imports
// ...
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyDataHolder {
@JsonProperty("name")
public String mName;
@JsonProperty("url")
public String mUrl;
}
And parse list of MyDataHolders
String jsonString = // your json
ObjectMapper mapper = new ObjectMapper();
List<MyDataHolder> list = mapper.readValue(jsonString,
new TypeReference<ArrayList<MyDataHolder>>() {});
Using list items
String firstName = list.get(0).mName;
String secondName = list.get(1).mName;
I'll just give a little Jackson example:
First create a data holder which has the fields from JSON string
// imports
// ...
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyDataHolder {
@JsonProperty("name")
public String mName;
@JsonProperty("url")
public String mUrl;
}
And parse list of MyDataHolders
String jsonString = // your json
ObjectMapper mapper = new ObjectMapper();
List<MyDataHolder> list = mapper.readValue(jsonString,
new TypeReference<ArrayList<MyDataHolder>>() {});
Using list items
String firstName = list.get(0).mName;
String secondName = list.get(1).mName;
edited Sep 24 '13 at 10:07
answered Sep 24 '13 at 10:01
vilpe89vilpe89
3,70112232
3,70112232
Can you give me an example how would i get JSON as a string from url i have, please ?
– SteBra
Sep 24 '13 at 10:37
2
Weeell here's something that I made up: pastebin.com/w4thyPVK
– vilpe89
Sep 24 '13 at 11:24
1
TheDefaultHttpClient
s execute-method is a blocking call so it has to be done in a background task, just to make clear why it is in separate class.
– vilpe89
Sep 24 '13 at 11:39
1
have you imported OnHttpGetCompletedListener? Like thisimport your_package_name_here.HttpGetTask.OnHttpGetCompletedListener;
– vilpe89
Sep 24 '13 at 12:45
1
Yeap, that was the problem... Thanks for solving my issue :)
– SteBra
Sep 24 '13 at 12:59
|
show 2 more comments
Can you give me an example how would i get JSON as a string from url i have, please ?
– SteBra
Sep 24 '13 at 10:37
2
Weeell here's something that I made up: pastebin.com/w4thyPVK
– vilpe89
Sep 24 '13 at 11:24
1
TheDefaultHttpClient
s execute-method is a blocking call so it has to be done in a background task, just to make clear why it is in separate class.
– vilpe89
Sep 24 '13 at 11:39
1
have you imported OnHttpGetCompletedListener? Like thisimport your_package_name_here.HttpGetTask.OnHttpGetCompletedListener;
– vilpe89
Sep 24 '13 at 12:45
1
Yeap, that was the problem... Thanks for solving my issue :)
– SteBra
Sep 24 '13 at 12:59
Can you give me an example how would i get JSON as a string from url i have, please ?
– SteBra
Sep 24 '13 at 10:37
Can you give me an example how would i get JSON as a string from url i have, please ?
– SteBra
Sep 24 '13 at 10:37
2
2
Weeell here's something that I made up: pastebin.com/w4thyPVK
– vilpe89
Sep 24 '13 at 11:24
Weeell here's something that I made up: pastebin.com/w4thyPVK
– vilpe89
Sep 24 '13 at 11:24
1
1
The
DefaultHttpClient
s execute-method is a blocking call so it has to be done in a background task, just to make clear why it is in separate class.– vilpe89
Sep 24 '13 at 11:39
The
DefaultHttpClient
s execute-method is a blocking call so it has to be done in a background task, just to make clear why it is in separate class.– vilpe89
Sep 24 '13 at 11:39
1
1
have you imported OnHttpGetCompletedListener? Like this
import your_package_name_here.HttpGetTask.OnHttpGetCompletedListener;
– vilpe89
Sep 24 '13 at 12:45
have you imported OnHttpGetCompletedListener? Like this
import your_package_name_here.HttpGetTask.OnHttpGetCompletedListener;
– vilpe89
Sep 24 '13 at 12:45
1
1
Yeap, that was the problem... Thanks for solving my issue :)
– SteBra
Sep 24 '13 at 12:59
Yeap, that was the problem... Thanks for solving my issue :)
– SteBra
Sep 24 '13 at 12:59
|
show 2 more comments
public static void main(String args) throws JSONException {
String str = "[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]";
JSONArray jsonarray = new JSONArray(str);
for(int i=0; i<jsonarray.length(); i++){
JSONObject obj = jsonarray.getJSONObject(i);
String name = obj.getString("name");
String url = obj.getString("url");
System.out.println(name);
System.out.println(url);
}
}
Output:
name1
url1
name2
url2
add a comment |
public static void main(String args) throws JSONException {
String str = "[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]";
JSONArray jsonarray = new JSONArray(str);
for(int i=0; i<jsonarray.length(); i++){
JSONObject obj = jsonarray.getJSONObject(i);
String name = obj.getString("name");
String url = obj.getString("url");
System.out.println(name);
System.out.println(url);
}
}
Output:
name1
url1
name2
url2
add a comment |
public static void main(String args) throws JSONException {
String str = "[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]";
JSONArray jsonarray = new JSONArray(str);
for(int i=0; i<jsonarray.length(); i++){
JSONObject obj = jsonarray.getJSONObject(i);
String name = obj.getString("name");
String url = obj.getString("url");
System.out.println(name);
System.out.println(url);
}
}
Output:
name1
url1
name2
url2
public static void main(String args) throws JSONException {
String str = "[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]";
JSONArray jsonarray = new JSONArray(str);
for(int i=0; i<jsonarray.length(); i++){
JSONObject obj = jsonarray.getJSONObject(i);
String name = obj.getString("name");
String url = obj.getString("url");
System.out.println(name);
System.out.println(url);
}
}
Output:
name1
url1
name2
url2
answered Sep 24 '13 at 9:12
Maxim ShoustinMaxim Shoustin
68.3k25181210
68.3k25181210
add a comment |
add a comment |
Create a class to hold the objects.
public class Person{
private String name;
private String url;
//Get & Set methods for each field
}
Then deserialize as follows:
Gson gson = new Gson();
Person person = gson.fromJson(input, Person.class); //input is your String
Reference Article: http://blog.patrickbaumann.com/2011/11/gson-array-deserialization/
I asume this "input" would be URL to that json? or is it something else?
– SteBra
Sep 24 '13 at 9:09
@Stebra, Exactly, I included that as a comment in the code.
– Kevin Bowersox
Sep 24 '13 at 9:09
This is a very simple solution. It should be the accepted one in my view
– FabioC
Nov 18 '17 at 11:38
add a comment |
Create a class to hold the objects.
public class Person{
private String name;
private String url;
//Get & Set methods for each field
}
Then deserialize as follows:
Gson gson = new Gson();
Person person = gson.fromJson(input, Person.class); //input is your String
Reference Article: http://blog.patrickbaumann.com/2011/11/gson-array-deserialization/
I asume this "input" would be URL to that json? or is it something else?
– SteBra
Sep 24 '13 at 9:09
@Stebra, Exactly, I included that as a comment in the code.
– Kevin Bowersox
Sep 24 '13 at 9:09
This is a very simple solution. It should be the accepted one in my view
– FabioC
Nov 18 '17 at 11:38
add a comment |
Create a class to hold the objects.
public class Person{
private String name;
private String url;
//Get & Set methods for each field
}
Then deserialize as follows:
Gson gson = new Gson();
Person person = gson.fromJson(input, Person.class); //input is your String
Reference Article: http://blog.patrickbaumann.com/2011/11/gson-array-deserialization/
Create a class to hold the objects.
public class Person{
private String name;
private String url;
//Get & Set methods for each field
}
Then deserialize as follows:
Gson gson = new Gson();
Person person = gson.fromJson(input, Person.class); //input is your String
Reference Article: http://blog.patrickbaumann.com/2011/11/gson-array-deserialization/
answered Sep 24 '13 at 9:08
Kevin BowersoxKevin Bowersox
79.5k14124166
79.5k14124166
I asume this "input" would be URL to that json? or is it something else?
– SteBra
Sep 24 '13 at 9:09
@Stebra, Exactly, I included that as a comment in the code.
– Kevin Bowersox
Sep 24 '13 at 9:09
This is a very simple solution. It should be the accepted one in my view
– FabioC
Nov 18 '17 at 11:38
add a comment |
I asume this "input" would be URL to that json? or is it something else?
– SteBra
Sep 24 '13 at 9:09
@Stebra, Exactly, I included that as a comment in the code.
– Kevin Bowersox
Sep 24 '13 at 9:09
This is a very simple solution. It should be the accepted one in my view
– FabioC
Nov 18 '17 at 11:38
I asume this "input" would be URL to that json? or is it something else?
– SteBra
Sep 24 '13 at 9:09
I asume this "input" would be URL to that json? or is it something else?
– SteBra
Sep 24 '13 at 9:09
@Stebra, Exactly, I included that as a comment in the code.
– Kevin Bowersox
Sep 24 '13 at 9:09
@Stebra, Exactly, I included that as a comment in the code.
– Kevin Bowersox
Sep 24 '13 at 9:09
This is a very simple solution. It should be the accepted one in my view
– FabioC
Nov 18 '17 at 11:38
This is a very simple solution. It should be the accepted one in my view
– FabioC
Nov 18 '17 at 11:38
add a comment |
In this example there are several objects inside one json array. That is,
This is the json array: [{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]
This is one object: {"name":"name1","url":"url1"}
Assuming that you have got the result to a String variable called jSonResultString:
JSONArray arr = new JSONArray(jSonResultString);
//loop through each object
for (int i=0; i<arr.length(); i++){
JSONObject jsonProductObject = arr.getJSONObject(i);
String name = jsonProductObject.getString("name");
String url = jsonProductObject.getString("url");
}
did you try this?
– TharakaNirmana
Sep 24 '13 at 9:16
Im having a hard time getting my JSONstring from URL. Maybe you could help with that?
– SteBra
Sep 24 '13 at 10:49
Send me the URL
– TharakaNirmana
Sep 24 '13 at 11:00
add a comment |
In this example there are several objects inside one json array. That is,
This is the json array: [{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]
This is one object: {"name":"name1","url":"url1"}
Assuming that you have got the result to a String variable called jSonResultString:
JSONArray arr = new JSONArray(jSonResultString);
//loop through each object
for (int i=0; i<arr.length(); i++){
JSONObject jsonProductObject = arr.getJSONObject(i);
String name = jsonProductObject.getString("name");
String url = jsonProductObject.getString("url");
}
did you try this?
– TharakaNirmana
Sep 24 '13 at 9:16
Im having a hard time getting my JSONstring from URL. Maybe you could help with that?
– SteBra
Sep 24 '13 at 10:49
Send me the URL
– TharakaNirmana
Sep 24 '13 at 11:00
add a comment |
In this example there are several objects inside one json array. That is,
This is the json array: [{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]
This is one object: {"name":"name1","url":"url1"}
Assuming that you have got the result to a String variable called jSonResultString:
JSONArray arr = new JSONArray(jSonResultString);
//loop through each object
for (int i=0; i<arr.length(); i++){
JSONObject jsonProductObject = arr.getJSONObject(i);
String name = jsonProductObject.getString("name");
String url = jsonProductObject.getString("url");
}
In this example there are several objects inside one json array. That is,
This is the json array: [{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]
This is one object: {"name":"name1","url":"url1"}
Assuming that you have got the result to a String variable called jSonResultString:
JSONArray arr = new JSONArray(jSonResultString);
//loop through each object
for (int i=0; i<arr.length(); i++){
JSONObject jsonProductObject = arr.getJSONObject(i);
String name = jsonProductObject.getString("name");
String url = jsonProductObject.getString("url");
}
answered Sep 24 '13 at 9:09
TharakaNirmanaTharakaNirmana
7,65973966
7,65973966
did you try this?
– TharakaNirmana
Sep 24 '13 at 9:16
Im having a hard time getting my JSONstring from URL. Maybe you could help with that?
– SteBra
Sep 24 '13 at 10:49
Send me the URL
– TharakaNirmana
Sep 24 '13 at 11:00
add a comment |
did you try this?
– TharakaNirmana
Sep 24 '13 at 9:16
Im having a hard time getting my JSONstring from URL. Maybe you could help with that?
– SteBra
Sep 24 '13 at 10:49
Send me the URL
– TharakaNirmana
Sep 24 '13 at 11:00
did you try this?
– TharakaNirmana
Sep 24 '13 at 9:16
did you try this?
– TharakaNirmana
Sep 24 '13 at 9:16
Im having a hard time getting my JSONstring from URL. Maybe you could help with that?
– SteBra
Sep 24 '13 at 10:49
Im having a hard time getting my JSONstring from URL. Maybe you could help with that?
– SteBra
Sep 24 '13 at 10:49
Send me the URL
– TharakaNirmana
Sep 24 '13 at 11:00
Send me the URL
– TharakaNirmana
Sep 24 '13 at 11:00
add a comment |
@Stebra See this example. This may help you.
public class CustomerInfo
{
@SerializedName("customerid")
public String customerid;
@SerializedName("picture")
public String picture;
@SerializedName("location")
public String location;
public CustomerInfo()
{}
}
And when you get the result; parse like this
List<CustomerInfo> customers = null;
customers = (List<CustomerInfo>)gson.fromJson(result, new TypeToken<List<CustomerInfo>>() {}.getType());
1
Result variable is your JSONArray String
– nilkash
Sep 24 '13 at 9:10
Would result variable be the url i need to fetch json from or already fetched json ?
– SteBra
Sep 24 '13 at 9:22
1
result string is already fetched JSONArray string
– nilkash
Sep 24 '13 at 9:25
by far the easiest solution
– Danilo Salvio
Sep 8 '16 at 3:35
add a comment |
@Stebra See this example. This may help you.
public class CustomerInfo
{
@SerializedName("customerid")
public String customerid;
@SerializedName("picture")
public String picture;
@SerializedName("location")
public String location;
public CustomerInfo()
{}
}
And when you get the result; parse like this
List<CustomerInfo> customers = null;
customers = (List<CustomerInfo>)gson.fromJson(result, new TypeToken<List<CustomerInfo>>() {}.getType());
1
Result variable is your JSONArray String
– nilkash
Sep 24 '13 at 9:10
Would result variable be the url i need to fetch json from or already fetched json ?
– SteBra
Sep 24 '13 at 9:22
1
result string is already fetched JSONArray string
– nilkash
Sep 24 '13 at 9:25
by far the easiest solution
– Danilo Salvio
Sep 8 '16 at 3:35
add a comment |
@Stebra See this example. This may help you.
public class CustomerInfo
{
@SerializedName("customerid")
public String customerid;
@SerializedName("picture")
public String picture;
@SerializedName("location")
public String location;
public CustomerInfo()
{}
}
And when you get the result; parse like this
List<CustomerInfo> customers = null;
customers = (List<CustomerInfo>)gson.fromJson(result, new TypeToken<List<CustomerInfo>>() {}.getType());
@Stebra See this example. This may help you.
public class CustomerInfo
{
@SerializedName("customerid")
public String customerid;
@SerializedName("picture")
public String picture;
@SerializedName("location")
public String location;
public CustomerInfo()
{}
}
And when you get the result; parse like this
List<CustomerInfo> customers = null;
customers = (List<CustomerInfo>)gson.fromJson(result, new TypeToken<List<CustomerInfo>>() {}.getType());
answered Sep 24 '13 at 9:09
nilkashnilkash
3,2462382151
3,2462382151
1
Result variable is your JSONArray String
– nilkash
Sep 24 '13 at 9:10
Would result variable be the url i need to fetch json from or already fetched json ?
– SteBra
Sep 24 '13 at 9:22
1
result string is already fetched JSONArray string
– nilkash
Sep 24 '13 at 9:25
by far the easiest solution
– Danilo Salvio
Sep 8 '16 at 3:35
add a comment |
1
Result variable is your JSONArray String
– nilkash
Sep 24 '13 at 9:10
Would result variable be the url i need to fetch json from or already fetched json ?
– SteBra
Sep 24 '13 at 9:22
1
result string is already fetched JSONArray string
– nilkash
Sep 24 '13 at 9:25
by far the easiest solution
– Danilo Salvio
Sep 8 '16 at 3:35
1
1
Result variable is your JSONArray String
– nilkash
Sep 24 '13 at 9:10
Result variable is your JSONArray String
– nilkash
Sep 24 '13 at 9:10
Would result variable be the url i need to fetch json from or already fetched json ?
– SteBra
Sep 24 '13 at 9:22
Would result variable be the url i need to fetch json from or already fetched json ?
– SteBra
Sep 24 '13 at 9:22
1
1
result string is already fetched JSONArray string
– nilkash
Sep 24 '13 at 9:25
result string is already fetched JSONArray string
– nilkash
Sep 24 '13 at 9:25
by far the easiest solution
– Danilo Salvio
Sep 8 '16 at 3:35
by far the easiest solution
– Danilo Salvio
Sep 8 '16 at 3:35
add a comment |
A few great suggestions are already mentioned.
Using GSON is really handy indeed, and to make life even easier you can try this website
It's called jsonschema2pojo and does exactly that:
You give it your json and it generates a java object that can paste in your project.
You can select GSON to annotate your variables, so extracting the object from your json gets even easier!
add a comment |
A few great suggestions are already mentioned.
Using GSON is really handy indeed, and to make life even easier you can try this website
It's called jsonschema2pojo and does exactly that:
You give it your json and it generates a java object that can paste in your project.
You can select GSON to annotate your variables, so extracting the object from your json gets even easier!
add a comment |
A few great suggestions are already mentioned.
Using GSON is really handy indeed, and to make life even easier you can try this website
It's called jsonschema2pojo and does exactly that:
You give it your json and it generates a java object that can paste in your project.
You can select GSON to annotate your variables, so extracting the object from your json gets even easier!
A few great suggestions are already mentioned.
Using GSON is really handy indeed, and to make life even easier you can try this website
It's called jsonschema2pojo and does exactly that:
You give it your json and it generates a java object that can paste in your project.
You can select GSON to annotate your variables, so extracting the object from your json gets even easier!
answered Nov 3 '14 at 15:56
TomCBTomCB
1,82262654
1,82262654
add a comment |
add a comment |
My case
Load From Server Example..
int jsonLength = Integer.parseInt(jsonObject.getString("number_of_messages"));
if (jsonLength != 1) {
for (int i = 0; i < jsonLength; i++) {
JSONArray jsonArray = new JSONArray(jsonObject.getString("messages"));
JSONObject resJson = (JSONObject) jsonArray.get(i);
//addItem(resJson.getString("message"), resJson.getString("name"), resJson.getString("created_at"));
}
Hope it help
p.s "jsonObject.getString("messages")" type is jsonArray; first exist answer is cannot be convert string to jsonArray; i'm fix it.
– bong jae choe
Nov 5 '15 at 5:10
add a comment |
My case
Load From Server Example..
int jsonLength = Integer.parseInt(jsonObject.getString("number_of_messages"));
if (jsonLength != 1) {
for (int i = 0; i < jsonLength; i++) {
JSONArray jsonArray = new JSONArray(jsonObject.getString("messages"));
JSONObject resJson = (JSONObject) jsonArray.get(i);
//addItem(resJson.getString("message"), resJson.getString("name"), resJson.getString("created_at"));
}
Hope it help
p.s "jsonObject.getString("messages")" type is jsonArray; first exist answer is cannot be convert string to jsonArray; i'm fix it.
– bong jae choe
Nov 5 '15 at 5:10
add a comment |
My case
Load From Server Example..
int jsonLength = Integer.parseInt(jsonObject.getString("number_of_messages"));
if (jsonLength != 1) {
for (int i = 0; i < jsonLength; i++) {
JSONArray jsonArray = new JSONArray(jsonObject.getString("messages"));
JSONObject resJson = (JSONObject) jsonArray.get(i);
//addItem(resJson.getString("message"), resJson.getString("name"), resJson.getString("created_at"));
}
Hope it help
My case
Load From Server Example..
int jsonLength = Integer.parseInt(jsonObject.getString("number_of_messages"));
if (jsonLength != 1) {
for (int i = 0; i < jsonLength; i++) {
JSONArray jsonArray = new JSONArray(jsonObject.getString("messages"));
JSONObject resJson = (JSONObject) jsonArray.get(i);
//addItem(resJson.getString("message"), resJson.getString("name"), resJson.getString("created_at"));
}
Hope it help
answered Nov 4 '15 at 7:56
bong jae choebong jae choe
1,3921918
1,3921918
p.s "jsonObject.getString("messages")" type is jsonArray; first exist answer is cannot be convert string to jsonArray; i'm fix it.
– bong jae choe
Nov 5 '15 at 5:10
add a comment |
p.s "jsonObject.getString("messages")" type is jsonArray; first exist answer is cannot be convert string to jsonArray; i'm fix it.
– bong jae choe
Nov 5 '15 at 5:10
p.s "jsonObject.getString("messages")" type is jsonArray; first exist answer is cannot be convert string to jsonArray; i'm fix it.
– bong jae choe
Nov 5 '15 at 5:10
p.s "jsonObject.getString("messages")" type is jsonArray; first exist answer is cannot be convert string to jsonArray; i'm fix it.
– bong jae choe
Nov 5 '15 at 5:10
add a comment |
Create a POJO Java Class for the objects in the list like so:
class NameUrlClass{
private String name;
private String url;
//Constructor
public NameUrlClass(String name,String url){
this.name = name;
this.url = url;
}
}
Now simply create a List of NameUrlClass and initialize it to an ArrayList like so:
List<NameUrlClass> obj = new ArrayList<NameUrlClass>;
You can use store the JSON array in this object
obj = JSONArray;//[{"name":"name1","url":"url1"}{"name":"name2","url":"url2"},...]
It's not clear whatobj = JSONArray;
is supposed to do. What isJSONArray
in this context? Where does it come from? What is the purpose of initializingobj
with anew ArrayList<NameUrlClass>
, when you replace it directly afterwards anyways?
– Max Vollmer
Oct 24 '18 at 11:38
add a comment |
Create a POJO Java Class for the objects in the list like so:
class NameUrlClass{
private String name;
private String url;
//Constructor
public NameUrlClass(String name,String url){
this.name = name;
this.url = url;
}
}
Now simply create a List of NameUrlClass and initialize it to an ArrayList like so:
List<NameUrlClass> obj = new ArrayList<NameUrlClass>;
You can use store the JSON array in this object
obj = JSONArray;//[{"name":"name1","url":"url1"}{"name":"name2","url":"url2"},...]
It's not clear whatobj = JSONArray;
is supposed to do. What isJSONArray
in this context? Where does it come from? What is the purpose of initializingobj
with anew ArrayList<NameUrlClass>
, when you replace it directly afterwards anyways?
– Max Vollmer
Oct 24 '18 at 11:38
add a comment |
Create a POJO Java Class for the objects in the list like so:
class NameUrlClass{
private String name;
private String url;
//Constructor
public NameUrlClass(String name,String url){
this.name = name;
this.url = url;
}
}
Now simply create a List of NameUrlClass and initialize it to an ArrayList like so:
List<NameUrlClass> obj = new ArrayList<NameUrlClass>;
You can use store the JSON array in this object
obj = JSONArray;//[{"name":"name1","url":"url1"}{"name":"name2","url":"url2"},...]
Create a POJO Java Class for the objects in the list like so:
class NameUrlClass{
private String name;
private String url;
//Constructor
public NameUrlClass(String name,String url){
this.name = name;
this.url = url;
}
}
Now simply create a List of NameUrlClass and initialize it to an ArrayList like so:
List<NameUrlClass> obj = new ArrayList<NameUrlClass>;
You can use store the JSON array in this object
obj = JSONArray;//[{"name":"name1","url":"url1"}{"name":"name2","url":"url2"},...]
answered Oct 24 '18 at 11:17
Devesh PradhanDevesh Pradhan
11
11
It's not clear whatobj = JSONArray;
is supposed to do. What isJSONArray
in this context? Where does it come from? What is the purpose of initializingobj
with anew ArrayList<NameUrlClass>
, when you replace it directly afterwards anyways?
– Max Vollmer
Oct 24 '18 at 11:38
add a comment |
It's not clear whatobj = JSONArray;
is supposed to do. What isJSONArray
in this context? Where does it come from? What is the purpose of initializingobj
with anew ArrayList<NameUrlClass>
, when you replace it directly afterwards anyways?
– Max Vollmer
Oct 24 '18 at 11:38
It's not clear what
obj = JSONArray;
is supposed to do. What is JSONArray
in this context? Where does it come from? What is the purpose of initializing obj
with a new ArrayList<NameUrlClass>
, when you replace it directly afterwards anyways?– Max Vollmer
Oct 24 '18 at 11:38
It's not clear what
obj = JSONArray;
is supposed to do. What is JSONArray
in this context? Where does it come from? What is the purpose of initializing obj
with a new ArrayList<NameUrlClass>
, when you replace it directly afterwards anyways?– Max Vollmer
Oct 24 '18 at 11:38
add a comment |
URL url = new URL("your URL");
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
//setting the json string
String finalJson = buffer.toString();
//this is your string get the pattern from buffer.
JSONArray jsonarray = new JSONArray(finalJson);
add a comment |
URL url = new URL("your URL");
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
//setting the json string
String finalJson = buffer.toString();
//this is your string get the pattern from buffer.
JSONArray jsonarray = new JSONArray(finalJson);
add a comment |
URL url = new URL("your URL");
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
//setting the json string
String finalJson = buffer.toString();
//this is your string get the pattern from buffer.
JSONArray jsonarray = new JSONArray(finalJson);
URL url = new URL("your URL");
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
//setting the json string
String finalJson = buffer.toString();
//this is your string get the pattern from buffer.
JSONArray jsonarray = new JSONArray(finalJson);
answered Jan 30 '16 at 14:31
boyke purnomoboyke purnomo
71
71
add a comment |
add a comment |
Old post I know, but unless I've misunderstood the question, this should do the trick:
s = '[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]';
eval("array=" + s);
for (var i = 0; i < array.length; i++) {
for (var index in array[i]) {
alert(array[i][index]);
}
}
2
Wrong language.
– Sotirios Delimanolis
Jun 30 '16 at 17:47
add a comment |
Old post I know, but unless I've misunderstood the question, this should do the trick:
s = '[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]';
eval("array=" + s);
for (var i = 0; i < array.length; i++) {
for (var index in array[i]) {
alert(array[i][index]);
}
}
2
Wrong language.
– Sotirios Delimanolis
Jun 30 '16 at 17:47
add a comment |
Old post I know, but unless I've misunderstood the question, this should do the trick:
s = '[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]';
eval("array=" + s);
for (var i = 0; i < array.length; i++) {
for (var index in array[i]) {
alert(array[i][index]);
}
}
Old post I know, but unless I've misunderstood the question, this should do the trick:
s = '[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]';
eval("array=" + s);
for (var i = 0; i < array.length; i++) {
for (var index in array[i]) {
alert(array[i][index]);
}
}
answered Nov 9 '15 at 10:34
JanusJanus
1
1
2
Wrong language.
– Sotirios Delimanolis
Jun 30 '16 at 17:47
add a comment |
2
Wrong language.
– Sotirios Delimanolis
Jun 30 '16 at 17:47
2
2
Wrong language.
– Sotirios Delimanolis
Jun 30 '16 at 17:47
Wrong language.
– Sotirios Delimanolis
Jun 30 '16 at 17:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18977144%2fhow-to-parse-json-array-not-json-object-in-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Try this to see if your json is valid json.parser.online.fr
– An-droid
Sep 24 '13 at 9:02
are you using any library for parsing json? Use gson library for json parsing.Much Helpfull.
– nilkash
Sep 24 '13 at 9:03
It is valid. I parsed it in an app i did for an iPhone And it's working. I just dont know how to do it in Android
– SteBra
Sep 24 '13 at 9:03
1
@nikash, yeah i noticed i could use gson. only problem is, all examples i found are for parsing a JSONObject not JSONArray
– SteBra
Sep 24 '13 at 9:04
Post your code that you have tried so far.It will be helpful to give a precise answer accordingly.
– Spring Breaker
Sep 24 '13 at 9:09