org.json.JSONException: No value for id - java

I'm trying to parse json with an edittext, make it match for an id, and load the data it carries in textview. Im not really sure how to go about this, whether im using the right JSON, or if im using the right way to determine the most efficiet way to handle this. When i enter '5' in the edittext, i get the error mentioned below:
Error:
org.json.JSONException: No value for id
The JSON from url being parsed:
{
"id": "5",
"first_name": "Larry",
"last_name": "Gonzales",
"email": "lgonzales4#msu.edu",
"country": "Japan",
"ip_address": "242.198.195.241"
}
Java:
try {
JSONObject e = new JSONObject();
JSONObject jsonobjectidloader = e.getJSONObject(TAG_ID);
if (jsonobjectidloader.equals(xyz)){
uid.setText(id3);
id3 = jsonobjectidloader.getString(TAG_ID);
name1.setText(fname);
fname = jsonobjectidloader.getString(TAG_FIRST_NAME);
name2.setText(lname);
lname = jsonobjectidloader.getString(TAG_LAST_NAME);
email1.setText(email);
email = jsonobjectidloader.getString(TAG_EMAIL);
ipaddres.setText(ipa);
ipa = jsonobjectidloader.getString(TAG_IP);
cou.setText(country);
country = jsonobjectidloader.getString(TAG_COUNTRY);
I hope you guys can help me with this, im really stumped right now.

JSONObject e = new JSONObject(response.toString());
String id3 = e.getString(TAG_ID);
Response is your json from server

Use following code.
JSONObject jsonobjectidloader= new JSONObject(YOUR_JSON_STRING);
if (jsonobjectidloader.equals(xyz)){
id3 = jsonobjectidloader.getString(TAG_ID);
uid.setText(id3);
fname = jsonobjectidloader.getString(TAG_FIRST_NAME);
name1.setText(fname);
lname = jsonobjectidloader.getString(TAG_LAST_NAME);
name2.setText(lname);
email = jsonobjectidloader.getString(TAG_EMAIL);
email1.setText(email);
ipa = jsonobjectidloader.getString(TAG_IP);
ipaddres.setText(ipa);
country = jsonobjectidloader.getString(TAG_COUNTRY);
cou.setText(country);

just use it as
String json = '{"id":"5","first_name":"Larry","last_name":"Gonzales","email":"lgonzales4#msu.edu","country":"Japan","ip_address":"242.198.195.241"}';
e = new JSONObject(json);
.....
you need to actually provide the string to jsonboject constructor as argument.

Related

How do I select an element in a complex JSON array in java

I can't seem to get the selection of elements in the array right. Or the syntax swears or something else. Here is what I am trying
{
"info": "info",
"user":[{
0:
"info":{
"name": "user 1"
}
}]
}
Here is my code and what i am trying to do
JSONObject jsonObject = new JSONObject(json_string);
System.out.println(jsonObject.getJSONArray("user").getJSONArray(0).getJSONArray("info").getString("name"));
And I don't get anything. Who can tell me how to do it right
Thanks everyone. I figured out how to do it here is the code)
JSONObject obj = new JSONObject(json_string);
JSONArray userArray = obj.getJSONArray("user");
JSONObject userInfo = userArray.getJSONObject(0).getJSONObject("info");
String reciver = userInfo.getString("name");

How to get data from JSONObject in Java

I am new in Java and I try to search and try many ways to get USER_NAME and PASSWORD in JSONObject
here my code:
JSONObject data = new JSONObject();
JSONObject input = inputHeader.generateInputHeader("ABCD001");
JSONObject jsonResponse = requestService.startRequestTransactionHttp(input);
if(jsonResponse.getString("RES_ID").toString().equals("000")) {
//here I want get USER_NAME and PASSWORD
}
Here is what jsonResponse respond
{ "RES_ID" : "000", "DATA" : { "PASSWORD" : "123456", "USER_NAME" : "abcd" }, "RES_MSG" : "OK!"}
please help me to solve it.
From docs, you should use:
JSONObject data = jsonResponse.getObject("DATA");
String username = data.getString("USER_NAME");
String password = data.getString("PASSWORD");
You can use following to access the nested objects -
JSONObject data= jsonResponse.getJSONObject("DATA");
data.getString("USER_NAME");
data.getString("PASSWORD");

Convert JSON response to Array format in Android

I am getting response from server in JSONArray format. I am not able to retrieve the contents of array also my JSONArray has no Square brackets.
I am passing the response in php as json_encode($array) in server side
response {
community = “worker”
communitystr = "<null>";
workspace = abs;
email = "<null>";
admin = false;
persona = "<null>";
userinfo = {
info = {
contact1 = {
firstname = “jon”;
lastname = “Doe”
phone = “9885678905”;
objectname = contact;
id = 9;
};
event1 = {
eventname = “party”;
description = "";
order = 6;
id = 4;
objectname = events;
};
files = {
filename = “sample”;
description = "";
order = 11;
id = 11;
objectname = files;
};
};
};
};
I checked many links and all have used JSONObject(). But same is not working for me.
How do I get each values in this JSON Response ?
You have to use
: instead of =
, instead of ;
...
Watch out following format:
{
"Herausgeber": "Xema",
"Nummer": "1234-5678-9012-3456",
"Deckung": 2e+6,
"Waehrung": "EURO",
"Inhaber":
{
"Name": "Mustermann",
"Vorname": "Max",
"maennlich": true,
"Hobbys": [ "Reiten", "Golfen", "Lesen" ],
"Alter": 42,
"Kinder": [],
"Partner": null
}
}
Your code seems like to be more JavaScript-Object like :-)
Your response is not valid JSON object.
You can validate the JSON via some online tool, like http://jsonlint.com/
Full specification can be found in RFC 7159 https://www.rfc-editor.org/rfc/rfc7159.
Basically you should look how to encode the values into JSON format in correct way. For that you can refer to PHP Array to JSON Array using json_encode();

Unable to get json parsing for specific field name

I am working on Yahoo Finance. Trying to parse json data from a url, such as Google finance data.
I am fetching data into a string "str" and then parsing the json data to reach the name field inside resources.
the json data is :
{
"list":{
"meta":{
"type":"resource-list",
"start":0,
"count":1
},
"resources":[
{
"resource":{
"classname":"Quote",
"fields":{
"name":"Alphabet Inc.",
"price":"710.489990",
"symbol":"GOOGL",
"ts":"1452891600",
"type":"equity",
"utctime":"2016-01-15T21:00:00+0000",
"volume":"3833751"
}
}
}
]
}
}
I am trying to use this code, but it is not working - need to reach the "name" field:
str4 = Client.execute(httpget, responseHandler);
//str holds the json data given above- checked.
JSONObject str1 = new JSONObject(str4);
JSONObject list = str1.getJSONObject("list");
JSONArray resources = list.getJSONArray("resorces");
JSONObject fields = resources.getJSONObject(1);
str2 = fields.getString("name");
I notices three issues with your code:
you have one item in your JSONArray, so you should retrieve item 0, not item 1.
you misspelled the word resources in your code.
Also, I think you didn't retrieve the fields correctly. This should do it:
JSONObject str1 = new JSONObject(str4);
JSONObject list = str1.getJSONObject("list");
JSONArray resources = list.getJSONArray("resources");
JSONObject fields = resources.getJSONObject(0).getJSONObject("resource").getJSONObject("fields");
str2 = fields.getString("name");

JSON Parsing in Android (Flickr API)

I've been looking extract image urls from the Flickr api for my android application.
More specifically, I need to parse the first owner and first id field for the following JSON
http://pastebin.com/NVKXdELx
Hence, I tried
JSONObject json = new JSONObject(jsonString);
JSONObject photos = json.getJSONObject("photos");
JSONObject photo = photos.getJSONObject("photo");
String picOwner = photo.getString("owner");
String picID = photo.getString("id");
To me, the logic makes sense (grab the row photos, then grab the row photo, then extract the owner field as a string). However, an error is thrown on
JSONObject photo = photos.getJSONObject("photo");
Am I overlooking something?
Note: The ultimate values I am looking for are
"id": "8637130199",
"owner": "93693022#N07"
The element identified by "photo" is a JSON Array, you need to use the JSONArray class
JSONObject json = new JSONObject(jsonString);
JSONObject photos = json.getJSONObject("photos");
JSONArray photo = photos.getJSONArray("photo");
if (photo.length() > 0) {
JSONObject first = photo.getJSONObject(0);
String picOwner = first.getString("owner");
String picID = first.getString("id");
}

Categories