JSONObject from string on Android - android

l get string with answer from server. l want to do it JSONObject.
l do
JSONObject jsonObj = new JSONObject(json);
in json has
"{"sentences":[{"trans":"R\u0455R\u0491ReR\u0405","orig":"�\u0455�\u0491��\u0405","translit":"","src_translit":"R\u1E91Rg\u0300RoR\u1E90"}],"src":"ru","server_time":1}"
but jsonObj has
"{"sentences":[{"src_translit":"RẑRg̀RoRẐ","orig":"�ѕ�ґ��Ѕ","trans":"RѕRґReRЅ","translit":""}],"server_time":1,"src":"ru"}"
so how l can get value from "trans":"RѕRґReRЅ"?
PS RѕRґReRЅ it's "own" in normal charset

If you already have the jsonObj you just have to do:
try {
// Getting Array of Sentences
sentences = json.getJSONArray("sentences");
// looping through All Contacts
for(int i = 0; i < sentences.length(); i++){
JSONObject c = sentences.getJSONObject(i);
// get the value from trans
String trans = c.getString("trans");
//now you should save this string in an array
}
} catch (JSONException e) {
e.printStackTrace();
}

try this
JSONObject jsonObj = new JSONObject(json);
JSONArray sentences = jsonObj.getJSONArray("sentences");
for(int i=0;i<sentences.length();i++){
JSONObject number = sentences.getJSONObject(i);
String transValue = number.getString("trans");
}

First you have to remove single quote form starting and from ending of your JSON string (json), by this your JSON will be a valid json for this you have to do like :
json= json.substring(1, json.length()-1);
After that you do like this :
JSONObject oJsonObject = new JSONObject(json);
JSONArray oJsonArray = oJsonObject.getJSONArray("sentences");
for(int i=0; i<oJsonArray.length(); i++)
{
JSONObject oJsonObject1 = oJsonArray.getJSONObject(i);
String transValue = oJsonObject1 .getString("trans");
}

Related

Difficult to identify json data

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 :)

org.json.JSONException: Find specific string from Json Object

I am working in Android and finding json from internet that looks like this:
JSONObject childObject=me.getJSONObject(pos);
String fisrtkey=childObject.getString("A");
JSONArray jsonArray=childObject.getJSONArray("c");
I want to find C21 that is in the A. See the json coming from request.
Can someone help me?
try {
JSONObject j=new JSONObject(data);
JSONArray c= null;
c = j.getJSONArray("This");
JSONObject item=c.getJSONObject(0);
JSONArray me=item.getJSONArray("me");
for(int pos=0;pos<me.length();pos++)
{
JSONObject childObject=me.getJSONObject(pos);
String fisrtkey=childObject.getString("A");
JSONArray jsonArray=childObject.getJSONArray("c");
}
} catch (JSONException e1) {
e1.printStackTrace();
}
//hope this will help you and also check json is invalid or not ,you missed
// bracket of jsonarray "me".
You have missed THIS json array while parsing. First get that and from that json object and then get ME json array. Something like this
JSONObject j = new JSONObject(data);
JSONArray c = j.getJSONArray("This");
JSONObject j1 = c.getJSONArray(0);
JSONArray d = j.getJSONArray("me");
for(int n = 0; n < c.length(); n++) {
JSONObject item = c.getJSONObject(n);
System.out.println(item.getString("A"));
}
Try this:
JSONObject j = new JSONObject(data);
JSONArray c = j.getJSONArray("me");
for(int n = 0; n < c.length(); n++) {
JSONObject person = (JSONObject) c.get(n );
String id = person.getString("A");
...
}

Retrieving Data with JSON in Android

My application connects to the web and retrieves a JSON file. I am having some issues retrieving the data that I need from this file. Here is the link to the JSON file: http://api.wunderground.com/api/f9d9bc3cc3834375/forecast/q/CA/San_Francisco.json
Here is a snippet of what it looks like:
I would like to get the value of the "period" variable in the first JSon object within the "forecastday" array, which should be 0. Here is how i'm looking at this. "forecastday" is the array, within that there are a number of JSon objects, each containing variables like "period","icon"....."pop".
In my code, I attempt to get the JSON array "forecastday" and then get the first Json object of the array, I then retrieve the value of "period" in that object and set it in a TextView:
protected void onPostExecute(String response) {
if(response == null) {
response = "THERE WAS AN ERROR";
}
progressBar.setVisibility(View.GONE);
try {
JSONObject jsonObj = new JSONObject(response);
JSONArray contacts = jsonObj.getJSONArray("forecastday");
JSONObject c = contacts.getJSONObject(0);
String period = c.getString("period");
responseView.setText(period);
} catch (JSONException e) {
e.printStackTrace();
}
}
When I run the code, nothing is being retrieved. I'm new to working with JSon and am wondering if I'm looking at this wrong. Please help.
Your "forecastday" JSONArray is inside the "txt_forecast" JSONObject that is inside the "forecast" JSONObject of your response, so you have to extract your JSONArray from this JSONObject and not from the root JSON response :
try {
JSONObject jsonObj = new JSONObject(response);
--> JSONObject forecatsObj = jsonObj.getJSONObject("forecast");
--> JSONObject txtForecatsObj = forecatsObj.getJSONObject("txt_forecast");
JSONArray contacts = txtForecatsObj.getJSONArray("forecastday");
...
You were close but it is incorrect, try like this:
JSONObject jsonObj = new JSONObject(response);
JSONObject forecast = jsonObj.getJSONObject("forecast");
JSONObject txtForecast = forecast.getJSONObject("txt_forecast");
JSONArray forecastDay = txtForecast.getJSONArray("forecastday");
//parse the first period value
String period = forecastDay.getJSONObject(0).getString("period");
responseView.setText(period);
Try this code :
try {
JSONObject jsonObj = new JSONObject(response);
JSONObject forecastObj = jsonObj.getJSONObject("forecast");
JSONObject txt_forecastObj = forecastObj.getJSONObject("txt_forecast");
JSONArray foracastdayArray = txt_forecastObj.getJSONArray("foracastday");
JSONObject oOjb0 = foracastdayArray.getJSONObject(0);
String period = oOjb0.getString("perioed");
}catch (Exception e){
}
This code works for me fine:
JSONObject jsonResponse = new JSONObject(responce);
JSONArray jsonMainNode = jsonResponse.optJSONArray("forecastday");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String period = jsonChildNode.optString("period");
responceView.setText(period);
}

Problems with getting values from JSON object

I'm trying to get data once user logged in successfully but I never get any of results, what I am doing is next:
// response is my request to server
JSONObject obj = new JSONObject(response);
Log.d("RESPONSE",obj.toString());
so in log I do see values, like:
04-19 11:28:16.729: D/RESPONSE(3162): {"data":[{"loses":3,"username":"benedict","level":1,"strength":15,"experience":null,"gold":10,"password":"benedict","intelligence":5,"agility":10,"wins":5}],"status":true}
but once I try to read username for example like this:
String username = obj.getString("username");
The code above ^ gives me nothing in my string..
Any help how I can retrieve data from JSONObject? Thanks!
That is because the username is present in the data object, which happens to be an JSONArray. Get the data array from the response object, traverse through each JSONObject in the array, and from each object, extract your username.
Something like this:-
JSONObject obj = new JSONObject(response);
JSONArray data = obj.getJSONArray("data");
for(int i=0;i<data.length();i++){
JSONObject eachData = data.getJSONObject(i);
System.out.println("Username= "+ eachData.getString("username"));
}
your field username is in array data. To access into this try :
JSONObject obj = new JSONObject(response);
JSONArray array = obj.getJSONArray("data");
for(int i = 0; i < array.length(); ++i){
JSONObject data = array.getJSONObject(i);
String username = data.getString("username");
}
You need to first get JSONArray which is data :
JSONArray data = null;
data = json.getJSONArray("data");
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
String username = c.getString("username");
}
You can get idea about parsing JSON from HERE
Try this...
try {
JSONObject object = new JSONObject(response);
JSONArray Jarray = object.getJSONArray("data");
for (int i = 0; i < Jarray.length(); i++) {
JSONObject Jasonobject = Jarray.getJSONObject(i);
String loose= Jasonobject.getString("loses");
String username=Jasonobject.getString("username");
.......
........
}
} catch (JSONException e) {
Log.e("log_txt", "Error parsing data " + e.toString());
}

How do I pull the string array from this json object?

I am trying to get a list of available numbers from the following json object, using the class from org.json
{
"response":true,
"state":1,
"data":
{
"CALLERID":"81101099",
"numbers":
[
"21344111","21772917",
"28511113","29274472",
"29843999","29845591",
"30870001","30870089",
"30870090","30870091"
]
}
}
My first steps were, after receiving the json object from the web service:
jsonObj = new JSONObject(response);
jsonData = jsonObj.optJSONObject("data");
Now, how do I save the string array of numbers?
use:
jsonObj = new JSONObject(response);
jsonData = jsonObj.optJSONObject("data");
JSONArray arrJson = jsonData.getJSONArray("numbers");
String[] arr = new String[arrJson.length()];
for(int i = 0; i < arrJson.length(); i++)
arr[i] = arrJson.getString(i);
you need to use JSONArray to pull data in an array
JSONObject jObj= new JSONObject(your_json_response);
JSONArray array = jObj.getJSONArray("data");
Assuming that you are trying to get it in a javascript block, Try something like this
var arrNumber = jsonData.numbers;
My code is for getting "data":
public void jsonParserArray(String json) {
String [] resultsNumbers = new String[100];
try {
JSONObject jsonObjectGetData = new JSONObject(json);
JSONObject jsonObjectGetNumbers = jsonObjectGetData.optJSONObject("results");
JSONArray jsonArray = jsonObjectGetNumbers.getJSONArray("numbers");
for (int i = 0; i < jsonArray.length(); i++) {
resultsNumbers[i] = jsonArray.getString(i);
}
} catch (JSONException e) {
e.printStackTrace();
Log.e(LOG_TAG, e.toString());
}
}

Categories

Resources