JSONObject data = response.getJSONObject("data");
Iterator x = data.keys();
JSONArray jsonArray = new JSONArray();
while (x.hasNext()){
String key = (String) x.next();
jsonArray.put(data.get(key));
}
Log.d("TZX", String.valueOf(jsonArray));
I have JSONObject on variable data like this
{"type":"DISKON","result":"DUPE","data":{"1":{"Code":"DISC2","Description":"DISC SALES","Value":0},"2":{"Code":"DISC1.5%","Description":"DISC1.5%","Value":1.5},"3":{"Code":"DISC 2%","Description":"DISC 2%","Value":2}}}
How to i get Object "Value" on JSONObject for send to JSONArray on variable "jsonArray"
Sorry for my grammar and I'm newbie
谢谢
try this,
JSONObject data = response.getJSONObject("data");
JSONArray jsonArray = new JSONArray();
Iterator<?> keys = data.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
if (data.get(key) instanceof JSONObject) {
jsonArray.put(((JSONObject) data.get(key)).getString("Value"));
}
}
Log.e("TZX", String.valueOf(jsonArray));
You are having array, so use JsonArray directly to parse data as in
JSONArray array = response.getJSONArray("data");
List<Integer> values = new ArrayList<>();
for(int i=0;i < array.length();i++){
values.add(array.getJSONObject(i).getInt("Value"));
}
You variable data is not JSONObject it's JSONArray.
data = [{"Code":"DISC2","Description":"DISC SALES","Value":0},{"Code":"DISC1.5%","Description":"DISC1.5%","Value":1.5},{"Code":"DISC 2%","Description":"DISC 2%","Value":2}]
To get JSONObject from array
JSONArray list = new JSONArray();
JSONObject obj = new JSONObject(data)
JSONArray jsonArray = new JSONArray(obj);
for(int i=0;i<json.length();i++){
JSONObject e = jsonArray.getJSONObject(i);
String value = e.getString("Value"));
list.put(value);
}
Related
Below is the query which is fetching the data from the MySql Database but the issue is that in browser it successfully outputs the data but in Android app it gives the error which is below
Java.Long.String Cannot Convert to JsonArray
if($stem=$con->prepare("select question from MCQs where c_id=$c_id ")){
$stem->execute ();
$stem->bind_result($question);
$budget=array();
while($stem->fetch())
{
$temp=array();
$temp['question']=$question;
array_push($budget,$temp);
}
echo json_encode($budget);
Below is the Android Side Java Code for the JSON
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
JSONArray jsonArray = jsonObject.getJSONArray("data");
if (success.equals("1")) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
String questions = jsonObject1.getString("question").trim();
final String opt0ne = jsonObject1.getString("option1").trim();
;
String opttwo = jsonObject1.getString("option2").trim();
;
String optthree = jsonObject1.getString("option3").trim();
final String correctt = jsonObject1.getString("correct").trim();
JSONObject json = new JSONObject(result);
JSONArray jArray = json.getJSONArray("data");
for (int i=0;jArray.length(); i++)
{
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
//try this
String msg= jsonObject1.getString("success");
}
JSONObject baseJsonResponse = new JSONObject(newsJSON);
JSONObject responseObj = baseJsonResponse.getJSONObject("response");
JSONArray resultArray = responseObj.getJSONArray("results");
for (int i =0;i<resultArray.length();i++){
String sectionName = resultArray.getString("sectionName");}
I want to get the section name for this url:
https://content.guardianapis.com/search?api-key=e11b8d10-d6ef-4fb7-9c12-094c58d37687
You can get Json Value Like this.
JSONObject baseJsonResponse = new JSONObject(newsJSON);
JSONObject responseObj = null;
try {
responseObj = baseJsonResponse.getJSONObject("response");
JSONArray resultArray = responseObj.getJSONArray("results");
int size = resultArray.length();
for (int i =0;i<size;i++){
JSONObject myJson = resultArray.getJSONObject(i);
String sectionName = myJson.getString("sectionName");
}
} catch (JSONException e) {
e.printStackTrace();
}
I have JSON from file.
{
"from_excel":[
{
"solution":"Fisrt",
"num":"1"
},
{
"solution":"Second",
"num":"2"
},
{
"solution":"third",
"num":"3"
},
{
"solution":"fourth",
"num":"4"
},
{
"solution":"fifth",
"num":"5"
}
]
}
and want to parse list of Solution and Num.
Used lib org.json.simple.*
Try this one
Object obj = parser.parse(new FileReader("E:\\json.txt"));
JSONObject jsonObject = (JSONObject) obj;
out.println(jsonObject.get("from_excel"));
JSONObject obj_new = (JSONObject) jsonObject.get("from_excel");
JSONArray solution = (JSONArray) obj_new.get("solution");
Iterator iterator = solution.iterator();
while (iterator.hasNext()) {
out.println(iterator.next());
}
What am I doing wrong?
If try to write
JSONObject solutions = (JSONArray) jsonObject.get("from_excel");
If try to write
JSONArray array = obj.getJSONArray("from_excel");
get error
working solution
JSONParser parser = new JSONParser();
JSONObject jsonObject;
try {
jsonObject = (JSONObject) parser.parse(new FileReader("E:\\json.txt"));
out.println("<br>"+jsonObject);
JSONArray from_excel = (JSONArray)jsonObject.get("from_excel");
// for row output 1
for(Object o: from_excel){
out.println("<br>"+o);
}
// for row output 2
Iterator iterator = from_excel.iterator();
while (iterator.hasNext()) {
out.println("<br>"+iterator.next());
}
// for item output 3
for (int i = 0; i < from_excel.size(); i++) {
JSONObject jsonObjectRow = (JSONObject) from_excel.get(i);
String num = (String) jsonObjectRow.get("num");
String solution = (String) jsonObjectRow.get("solution");
out.println("<br>num="+num+"; solution="+solution);
}
} catch (Exception e) {
out.println("Error: "+e);
}
There is no "solution" array in the JSON. "from_excel" is the array. So your code should look like this :
Object obj = parser.parse(new FileReader("E:\\json.txt"));
JSONObject jsonObject = (JSONObject) obj;
out.println(jsonObject.get("from_excel"));
JSONArray solutions = (JSONArray) jsonObject.get("from_excel");
Iterator iterator = solutions.iterator();
while (iterator.hasNext()) {
out.println(iterator.next());
}
from_excel is JSON array not an object. So you should achieve it is array.
JSONObject jsonObject = (JSONObject) obj;
JSONArray array = obj.getJSONArray("from_excel");
then iterate the array and get each jsonobject. Something like the below
for(int i = 0 ; i < array.length() ; i++){
array.getJSONObject(i).getString("solution");
}
I submit a query from my Java application, which upon running on Elasticsearch server returns the result in the form of a string. I want the result as a list of JSONObject objects. I can convert the string to a JSONObject using JSONObject jsonResponse = new JSONObject(responseString).
Is there any method by which I can get this in the form of a List<JSONObject>?
Instead of using JSONObject you may use JSONArray. If you really need to convert it to a List you may do something like:
List<JSONObject> list = new ArrayList<JSONObject>();
try {
int i;
JSONArray array = new JSONArray(string);
for (i = 0; i < array.length(); i++)
list.add(array.getJSONObject(i);
} catch (JSONException e) {
System.out.println(e.getMessage());
}
There is an answer of your question:
https://stackoverflow.com/a/17037364/1979882
ArrayList<String> listdata = new ArrayList<String>();
JSONArray jArray = (JSONArray)jsonObject;
if (jArray != null) {
for (int i=0;i<jArray.length();i++){
listdata.add(jArray.get(i).toString());
}
}
This method is really easy and works too
try {
JSONObject jsonObject = new JSONObject(THESTRINGHERE);
String[] names = JSONObject.getNames(jsonObject);
JSONArray jsonArray = jsonObject.toJSONArray(new JSONArray(names));
ArrayList<String> listdata = new ArrayList<String>();
JSONArray jArray = (JSONArray)jsonArray;
if (jArray != null) {
for (int i=0;i<jArray.length();i++){
listdata.add(jArray.get(i).toString());
}
}
// System.out.println(listdata);
} catch (Exception e) {
System.out.println(e.getMessage());
}
I have a webservice which returns a JSON array in this format:
[{"imageid":"3","userid":"1","imagepath":"SLDFJNDSKJFN","filterid":"1","dateadded":"2014-05-06 21:20:18.920257","public":"t"},
{"imageid":"4","userid":"1","imagepath":"dsfkjsdkfjnkjdfsn","filterid":"1","dateadded":"2014-05-06 21:43:37.642748","public":"t"}]
I need to get all the attributes seperately? How would I do this?
I know how to do it with JSONObject if there is just 1 thing being returned, but how does it work when multiple items are returned?
Thanks
try {
JSONArray jArray = new JSONArray(jsonString);
String s = new String();
for (int i = 0; i < jArray.length(); i++) {
s = jArray.getJSONObject(i).getString("imageid").toString();
s = jArray.getJSONObject(i).getString("userid").toString();
}
} catch (JSONException je) {
}
Create an Object class with all variables, create a List for this Object, add all objects in your JSONArray to the list, use the one you need.
List<YourObject> objList = new ArrayList<YourObject>();
JSONArray a = new JSONArray(response);
int size = a.length();
for (int i=0 ; i<size ; i++){
JSONObject aa = a.getJSONObject(i);
String id = aa.getString("imageid");
String userid = aa.getString("userid");
String imagepath = aa.getString("imagepath");
String filterid = aa.getString("filterid");
String dateadded = aa.getString("dateadded");
String publicText = aa.getString("public");
YourObject obj = new YourObject(id,userid,imagepath,filterid,dateadded,publicText);
objList.add(obj);
}
So what you are having here is some JSON objects inside a JSON array.
What you want to do is this:
JSONArray array = ...;
for (int i = 0; i < array.length(); i++) {
JSONObject o = array.getJSONObject(i);
// Extract whatever you want from the JSON object.
}
I hope it helped.
You can use JSONArray to parse array of JSON response.
private void parseJsonArray(String response) {
try {
JSONArray array = new JSONArray(response);
for(int i=0;i<array.length();i++){
JSONObject jsonObject = array.getJSONObject(i);
String ImageId = jsonObject.getString("imageid");
Log.v("JSON Parser", "ImageId: "+ImageId);
}
} catch (Exception e) {
e.printStackTrace();
}
}