This is My Json String
{"Damages":[{"id":15,"rf_no":5,"state":"Print5","dmg_no":0,"town":"NEWASA","date":"16\/08\/2015","firm_name":"SHREE ENTERPRISES (NEWASE) "},{"id":36,"rf_no":7,"state":"Print7","dmg_no":0,"town":"NEWASA","date":"16\/08\/2015","firm_name":"SHREE ENTERPRISES (NEWASE) "}]}
But in Android Application its Showing No Value For Damages
This is My Android Code
JSONArray jArray = new JSONArray(json.getString("Damages"));
for (int i = 0; i < jArray.length(); i++) {
JSONObject c = jArray.getJSONObject(i);
final String id = c.getString("id");
String dmg_no =
c.getString("dmg_no");
String firm_name = c.getString("firm_name");
final String rf_no = c.getString("rf_no");
String town=c.getString("town");
String date=c.getString("date");
String State=c.getString("state");
}
Help Me Please. And Thanx In Advance.
You can try this code, to parse your json data:
try
{
JSONObject jsonObj=new JSONObject(result); // result=JSON string
if(jsonObj.has("Damages"))
{
JSONArray arrayObj=jsonObj.getJSONArray("Damages");
for(int i=0;i<arrayObj.length();i++)
{
JSONObject childArray=arrayObj.getJSONObject(i);
Log.e("", "ID "+childArray.getString("id"));
Log.e("", "Ref No"+childArray.getString("rf_no"));
// similarly you can parse rest of your tags
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Hope this helps you.
This is your mistake : JSONArray jArray = new JSONArray(json.getString("Damages"));
you are trying to get the string by name "Damages" and coverting it to JSONarray. But what you have to do is you have to convert your string to JSONObject first and then get the array named "Damages" from that json object.
Try this
String jsonString = {"Damages":[{"id":15,"rf_no":5,"state":"Print5","dmg_no":0,"town":"NEWASA","date":"16\/08\/2015","firm_name":"SHREE ENTERPRISES (NEWASE) "},{"id":36,"rf_no":7,"state":"Print7","dmg_no":0,"town":"NEWASA","date":"16\/08\/2015","firm_name":"SHREE ENTERPRISES (NEWASE) "}]}
try {
JSONObject jsonObject=new JSONObject(jsonString);
JSONArray damageArray=jsonObject.getJSONArray("Damages");
for(int i=0;i<damageArray.length();i++)
{
JSONObject obj=damageArray.getJSONObject(i);
String dmg_no = obj.getString("dmg_no");
String firm_name = obj.getString("firm_name");
final String rf_no = obj.getString("rf_no");
String town=obj.getString("town");
String date=obj.getString("date");
String State=obj.getString("state");
}
} catch (JSONException e) {
e.printStackTrace();
}
This is the problem line
JSONArray jArray = new JSONArray(json.getString("Damages"));
What are you doing is telling the program to find value of String Damages and then get array with name of that value - but there is no array called like that! Your array is already named, and the name is Damages.
To get the array you simply do
JSONArray jArray = json.getJSONArray("Damages");
assuming json is the main response you are getting
Related
I have this code to get all information from website, I did it very well and it works, but I got stuck when trying to get "fields" from the site
This is the site url:
http://content.guardianapis.com/search?order-by=newest&show-references=author&show-tags=contributor&q=technology&show-fields=thumbnail&api-key=test
Here is the code and how can I fix it
try {
JSONObject jsonRes = new JSONObject(response);
JSONObject jsonResults = jsonRes.getJSONObject("response");
JSONArray resultsArray = jsonResults.getJSONArray("results");
for (int i = 0; i < resultsArray.length(); i++) {
JSONObject oneResult = resultsArray.getJSONObject(i);
String url = oneResult.getString("webUrl");
String webTitle = oneResult.getString("webTitle");
String section = oneResult.getString("sectionName");
String date = oneResult.getString("webPublicationDate");
date = formatDate(date);
JSONArray fields = oneResult.getJSONArray("fields");
JSONArray fieldsArray=oneResult.getJSONArray("fields");
String imageThumbnail= null;
if(fields.length()>0){
imageThumbnail=fields.getJSONObject(0).getString("thumbnail");
}
resultOfNewsData.add(new News(webTitle url, date, section, imageThumbnail));
}
} catch (JSONException e) {
Log.e("FromLoader", "Err parsing response", e);
}
Because the fields object isn't an Array is a JSON object
"fields":{"thumbnail":"https://media.guim.co.uk/fa5ae6ca7c78fdfc4ac0fe4212562e6daf4dfb3d/0_265_4032_2419/500.jpg"}
An array object should contain [ JSON1, JSON2, JSON3 ]
In your case this
JSONArray fields = oneResult.getJSONArray("fields");
becomes this
JSONObject fields = oneResult.getJSONObject("fields");
And I don't understand why are you getting the same data twice - fields and fieldsArray
I have json data as mentioned below.
{
"data":[
{
"Products":{
"id":"86",
"pname":"mi4",
"pcat":"9",
"subcat":"8",
"seccat":"0",
"oproduct":"1",
"pdetails":"Good phone",
"pprice":"10000",
"pdiscount":"10",
"qty":"1",
"qtytype":"GM",
"dcharge":"40",
"pimage":null,
"sname":"Easydeal",
"sid":"1100",
"size":"",
"pincode":""
}
}
]
}
I can identify array as getJSONArray("datas"). But I want to get pname and sname values.
Just reach the object
JSONObject resp=new JSONObject("response");
JSONArray data=resp.getJSONArray("data");
now if you want to get object at a particular index(say '0')
JSONObject objAt0=data.getJSONObject(0);
JSONObject products=objAt0.getJSONObject("products");
String pName=products.getString("pname");
you can similarly traverse the array
for(int i=0;i<data.lenght();i++){
JSONObject objAtI=data.getJSONObject(i);
JSONObject products=objAtI.getJSONObject("products");
String pName=products.getString("pname");
}
To get the to the key "Products" you should do:
JSONObject productsObject = YOUROBJECTNAME.getJSONArray("data").getJSONObject(0).getJSONObject("Products");
Then to get the values in productsObject you should do:
productsObject.getString("id");
productsObject.getString("pdetails");
And so on.
Try out the following code:
JSONObject object = new JSONObject(result);
JSONArray array = object.getJSONArray("data");
JSONObject object1 = array.getJSONObject(0);
JSONObject products = object1.getJSONObject("Products");
int id = object1.getInt("id");
String pname = object1.getString("pname");
This is how you get pname and sname:
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray;
try {
jsonArray = jsonObject.getJSONArray("data");
for(int counter = 0; counter <jsonArray.length(); counter++){
JSONObject jsonObject1 = jsonArray.getJSONObject(counter);
JSONObject products = jsonObject1.getJSONObject("Products");
String pname = products.getString("pname");
String sname = products.getString("sname");
}
} catch (JSONException e) {
e.printStackTrace();
}
PS: Poor JSON structure :)
I have a json with colon between the strings, and I'm not sure how can I parse it. I know that I don't have an array in the json, but I'm not sure how can I get the values...
{
"config": {
"network": {
"hni:21407" : "num:[INTNUM]",
"hni:311490" : "num:044[INTNUM]"
}
}
}
This is what I'm trying, but I never go through the loop for, and not really sure if I need it.
JSONObject obj = new JSONObject(netWorkJson);
String arr = obj.optString("network");
for(int i = 0; i < arr.length(); i++) {
String hni = obj.getString("hni");
String num = obj.getString("num");
}
Thanks in advance
You first need to parse the inner json object "network", after that you can loop over it's keys and get the values for them one by one:
private void parseJSON(String netWorkJson) throws JSONException {
JSONObject obj = new JSONObject(netWorkJson);
JSONObject config = obj.getJSONObject("config");
JSONObject network = config.getJSONObject("network");
Iterator<?> keys = network.keys();
while(keys.hasNext()) {
String key = (String) keys.next();
String value = network.getString(key);
}
}
Beauty of this is that it will also work if you had 100 hni values for example, and that you don't have to get them one by one.
network is JSONObject instead of JSONArray, so no need to use for-loop for getting value from it.just use do it as:
JSONObject obj = new JSONObject(netWorkJson);
// get network JSONObject from obj
JSONObject network=obj.getJSONObject("network");
// get both values from network object
String strHni=network.optString("hni:21407");
String strNum =network.optString("hni:311490");
JSONObject message = new JSONObject(config);
String value=message.getJSONObject("network").getString("hni:21407")
Try This
try {
JSONObject jsonObject = new JSONObject("config");
JSONArray jsonArray = jsonObject.getJSONArray("network");
for(int i =0;i<jsonArray.length();i++){
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
String hni21407 = jsonObject1.getString("hni:21407");
String hni311490 = jsonObject1.getString("hni:311490");
}
} catch (JSONException e) {
e.printStackTrace();
}
I am receiving a String response from the following #Override method
#Override
publc void onSuccess(String response) {
....
}
The conflict I am facing is that I do not know how to break up this response into key value pairings. This is an example of the response.
{"action":{"generic_message":"test generic message"},"domains":{"key_example_one":"https:/google.com","api_root_url":"https://test.com/new/0.2/json"},"page":null}}
I have attempted to convert the string to a JSONObject, and then adding the JSONObjects to a JSONArray.
JSONObject mJObj;
try {
mJObj = new JSONObject(response);
JSONArray mJArry = mJObj.getJSONArray("action");
for (int i = 0; i < mJArry.length(); i++) {
JSONObject newObj = mJArry.getJSONObject(i);
String test2 = newObj.getString("generic_example_for_all_platforms");
}
} catch (JSONException e) {
e.printStackTrace();
}
EDIT: I am getting JSON exception that JSONArray cannot be converted to JSONObject, for the following line.
JSONArray mJArry = mJObj.getJSONArray("action");
Thanks in advante
You do want to read more about JSON here.
The first thing to know is that {} is equal to a Json Object and [] is equal to a Json Array.
try {
JSONObject mJObj = new JSONObject(response);
JSONObject actionJsonObject = mJObj.getJSONObject("action");
String generic_message = actionJsonObject.getString("generic_message");
JSONObject domainsJsonObject = mJObj.getJSONObject("domains");
String key_example_one = domainsJsonObject.getString("key_example_one");
String api_root_url = domainsJsonObject.getString("api_root_url");
String page = mJObj.getString("page");
} catch (JSONException e) {
e.printStackTrace();
}
Check out Gson.
JsonObject jsonObject = new JsonParser().parse(jsonString).getAsJsonObject();
EDIT:
Just saw that you can't move to other Json processors. The property 'action' holds a JSONObject not a JSONArray. What about JSONObject jsonObject = mJObj.getJSONObject("action").
i have this json:
[{"id":"1","name":"john"},{"id":"2","name":"jack"},{"id":"3","name":"terry"}]
how i can parse this? i have to use a loop for extracting each group? for simple jsons i use this code:
public static String parseJSONResponse(String jsonResponse) {
try {
JSONObject json = new JSONObject(jsonResponse);
// get name & id here
String name = json.getString("name");
String id = json.getString("id");
} catch (JSONException e) {
e.printStackTrace();
}
return name;
}
but now i have to parse my new json. please help me
It should be like this:
public static String parseJSONResponse(String jsonResponse) {
try {
JSONArray jsonArray = new JSONArray(jsonResponse);
for (int index = 0; index < jsonArray.length(); index++) {
JSONObject json = jsonArray.getJSONObject(index);
// get name & id here
String name = json.getString("name");
String id = json.getString("id");
}
} catch (JSONException e) {
e.printStackTrace();
}
return name;
}
Of course you should return an array of names or whatever you want..
This is meant to be parsed by a JSONArray, and then each "record" is a JSONObject.
You can loop on the array and then retrieve the JSON String of each record with the getString(int) method. Then use this string to build a JSONObject, and just extract values like you do now.
You can use the following code:
public static void parseJSONResponse(String jsonResponse) {
try {
JSONArray jsonArray = new JSONArray(jsonResponse);
if(jsonArray != null){
for(int i=0; i<jsonArray.length(); i++){
JSONObject json = jsonArray.getJSONObject(i);
String name = json.getString("name");
String id = json.getString("id");
//Store strings data or use it
}
}
}catch (JSONException e) {
e.printStackTrace();
}
}
You need to modify the loop to store or use the data.
Hope it helps.