I'm having trouble with creating fmd from byte. Error with fmd is invalid.
Here is my code
String id = jsonobject.getString("fps_thumb");
byte [] encodeByte= Base64.decode(id,Base64.DEFAULT);
Fmd m_fmdAuxiliar = UareUGlobal.GetImporter().ImportFmd(encodeByte, Fmd.Format.ANSI_378_2004, Fmd.Format.ISO_19794_2_2005);
Does anyone know?
this is the code for retrieving data from api and compare it from the reader finger print
try{
JSONArray jsonarray = new JSONArray(http.getResponse());
for(int i=0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String id = jsonobject.getString("fps_thumb");
byte[] bytes = id.getBytes(StandardCharsets.UTF_8);
try {
Fmd m_fmdAuxiliar = UareUGlobal.GetImporter().ImportFmd(bytes, Fmd.Format.ANSI_378_2004, Fmd.Format.ANSI_378_2004);
m_score = m_engine.Compare(m_fmdAuxiliar, 0,m_engine.CreateFmd(cap_result.image, Fmd.Format.ANSI_378_2004), 0);
DecimalFormat formatting = new DecimalFormat("##.######");
m_text_conclusionString = "Dissimilarity Score: " + String.valueOf(m_score)+ ", False match rate: " +
Double.valueOf(formatting.format((double)m_score/0x7FFFFFFF)) + " (" + (m_score < (0x7FFFFFFF/100000)
? "match" : "no match") + ")";
Log.i("TAG",m_text_conclusionString);
} catch (UareUException e) {
e.printStackTrace();
}
}
}
catch (JSONException e){
e.printStackTrace();
}
Related
This is the function that's giving me the problem:
public String URLToJson() {
String result = "";
String jsonString = ReadingURL(" here goes my URL that reads a JSON ");
JSONObject jsonResult = null;
try {
jsonResult = new JSONObject(jsonString);
JSONArray data = jsonResult.getJSONArray("Configuracion");
if (data != null) {
for (int i = 0; i <= data.length(); i++) {
result = result + "Dirección: " + data.getJSONObject(i).getString("Direccion") + "\n";
result = result + "Cédula: " + data.getJSONObject(i).getString("Cedula") + "\n";
result = result + "Nombre: : " + data.getJSONObject(i).getString("Nombre") + "\n";
result = result + "Teléfono : " + data.getJSONObject(i).getString("Telefono") + "\n";
result = result + "Hacienda: " + data.getJSONObject(i).getString("Hacienda") + "\n";
}
}
return result;
}catch (JSONException e){
e.printStackTrace();
return "Error Reading JSON Data";
}
}
And then this comes up:
`W/System.err: org.json.JSONException: Value {"Direccion":"Somewhere","Cedula":"111111","Nombre":"Something","Telefono":"2222-2440","Hacienda":"Something"} at Configuracion of type org.json.JSONObject cannot be converted to JSONArray
at org.json.JSON.typeMismatch(JSON.java:100)
W/System.err: at org.json.JSONObject.getJSONArray(JSONObject.java:588)
at com.example.user.mypos.PrintManager.URLToJson(PrintManager.java:977)
W/System.err: at com.example.user.mypos.PrintManager$4.run(PrintManager.java:917)
at java.lang.Thread.run(Thread.java:818)W/System.err: org.json.JSONException: Value { the values that are supposed to be } of type org.json.JSONObject cannot be converted to JSONArray`
ReadingURL basically reads the content of an URL, that has the JSON in String.
From the exception it's clear that the JSON string returned by the URL is of type JSONObject not of JSONArray .
Value { the values that are supposed to be } of type org.json.JSONObject cannot be converted to JSONArray
JSON object will starts with { & ends with }
{
"KEY1":"VALUE1",
"KEY2":"VALUE2"
}
and JSON array will starts with [ and ends with ] .
[
{"KEY1":"VALUE1","KEY2":"VALUE2"},{"KEY1":"VALUE1","KEY2":"VALUE2"}
]
So you are getting this exception because you are trying to convert JSON object to JSON array.
to Deepak Gunasekaran
public String URLToJson() {
String result = "";
String jsonString = ReadingURL("http://deliciasmarinas.avancari.co.cr/app/tiquete.php?factura=414696772");
JSONObject jsonResult = null;
try {
jsonResult = new JSONObject(jsonString);
for (int i = 0; i <= jsonResult.length(); i++) {
result = result + "Dirección: " + jsonResult.get("Direccion") + "\n";
result = result + "Cédula: " + jsonResult.get("Cedula") + "\n";
result = result + "Nombre: : " + jsonResult.get("Nombre") + "\n";
result = result + "Teléfono : " + jsonResult.get("Telefono") + "\n";
result = result + "Hacienda: " + jsonResult.get("Hacienda") + "\n";
}
return result;
}catch (JSONException e){
e.printStackTrace();
return "Error Reading JSON Data";
}
}
And now it just shows
W/System.err: org.json.JSONException: No value for Direccion
at org.json.JSONObject.get(JSONObject.java:389)
W/System.err: at com.example.user.mypos.PrintManager.URLToJson(PrintManager.java:978)
at com.example.user.mypos.PrintManager$4.run(PrintManager.java:917)
at java.lang.Thread.run(Thread.java:818)
working on an android application to try and connect to "steemit.com' and return JSON data to me.
Everything has been working so far, printing the mass response from the URL to the Textview, only I am now getting no errors, and no text printed out in the screen so I assume I am using the wrong type of object or something. Perhaps the data I am trying to retrieve is not an Array? What do you all think? Here is my code.
public class fetchdata extends AsyncTask<Void,Void,Void> {
String data = "";
String dataParsed = "";
String singleParsed = "";
#Override
protected Void doInBackground(Void... voids) {
{
try {
URL url = new URL("https://steemit.com/#curie.json");
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
InputStream inputStream = httpsURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String lines = "";
while(lines != null){
lines = bufferedReader.readLine();
data = data + lines;
}
JSONArray JA = new JSONArray(data);
for (int i =0 ;i < JA.length(); i++){
JSONObject JO = (JSONObject) JA.get(i);
singleParsed = "User: " + JO.get("user") + "\n" +
"Location: " + JO.get("location") + "\n" +
"ID: " + JO.get("id")+"\n";
dataParsed = dataParsed + singleParsed;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
followers.dataTV.setText(this.dataParsed);
}
}
and the page I expect the TextView to display data on.
public class followers extends AppCompatActivity {
public static TextView dataTV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_followers);
ListView followList = (ListView)findViewById(R.id.followList);
dataTV = (TextView)findViewById(R.id.followersTVData);
fetchdata process = new fetchdata();
process.execute();
}
}
If I have not been clear, what the issue is, is that when I 'printText' using the variable 'data', there is no problems, and the bulk text is printed, however, I am now trying to break it down into bits, and it is just not printing anything when I use the variable 'dataParsed'. Any help is appreciated. Thank you in advance!
I have been asked for the response. Here it is, though rather long.
{"user":{"id":1026971,"name":"ceruleanblue","owner":{"weight_threshold":1,"account_auths":[],"key_auths":[["STM7UPr1LJMw4aAxcuiYAmad6bjjiaeDcfgSynRMrr5L6uvuSJLDJ",1]]},"active":{"weight_threshold":1,"account_auths":[],"key_auths":[["STM7qUaQCghsFZA37fTxVB4BqBBK49z35ni6pha1Kr4q4qLkrNRyH",1]]},"posting":{"weight_threshold":1,"account_auths":[["minnowbooster",1],["steemauto",1]],"key_auths":[["STM7qF27DSYNYjRu5Jayxxxpt1rtEoJLH6c1ekMwNpcDmGfsvko6z",1]]},"memo_key":"STM7wNQdNS9oPbVXscbzn7vfzjB7SwmLGQuFQNzZgatgpqvdKzWQZ","json_metadata":{"profile":{"profile_image":"https://cdn.steemitimages.com/DQmfNj7SLU1aBtV9UkJa5ZKMZPNuzR4ei5UJRA54JxFk99M/Mushrooms%20Trippy%20Art%20Fabric%20Cloth%20Rolled%20Wall%20Poster%20Print.jpg","name":"Cerulean's Chillzone","about":"IT Technician, Programmer, Day Trader, Night Toker.","location":"Ontario, Canada","cover_image":"https://cdn.steemitimages.com/DQmTwT379V7EcQ1ZkqkmJkpWyu4QXw1LzDinv9uoyixksMY/tumblr_static_tumblr_static__640.jpg"}},"proxy":"","last_owner_update":"2018-06-18T19:57:39","last_account_update":"2018-08-01T04:33:06","created":"2018-06-03T20:28:21","mined":false,"recovery_account":"steem","last_account_recovery":"1970-01-01T00:00:00","reset_account":"null","comment_count":0,"lifetime_vote_count":0,"post_count":321,"can_vote":true,"voting_power":9800,"last_vote_time":"2018-08-09T02:47:03","balance":"8.000 STEEM","savings_balance":"0.000 STEEM","sbd_balance":"1.979 SBD","sbd_seconds":"927621285","sbd_seconds_last_update":"2018-08-09T13:23:15","sbd_last_interest_payment":"2018-07-11T10:18:12","savings_sbd_balance":"0.000 SBD","savings_sbd_seconds":"2067163545","savings_sbd_seconds_last_update":"2018-07-23T08:58:48","savings_sbd_last_interest_payment":"2018-07-09T06:32:27","savings_withdraw_requests":0,"reward_sbd_balance":"0.000 SBD","reward_steem_balance":"0.000 STEEM","reward_vesting_balance":"0.000000 VESTS","reward_vesting_steem":"0.000 STEEM","vesting_shares":"167703.513691 VESTS","delegated_vesting_shares":"29412.000000 VESTS","received_vesting_shares":"0.000000 VESTS","vesting_withdraw_rate":"0.000000 VESTS","next_vesting_withdrawal":"1969-12-31T23:59:59","withdrawn":0,"to_withdraw":0,"withdraw_routes":0,"curation_rewards":182,"posting_rewards":110408,"proxied_vsf_votes":[0,0,0,0],"witnesses_voted_for":1,"last_post":"2018-08-07T12:43:42","last_root_post":"2018-08-07T12:25:39","average_bandwidth":"44620566375","lifetime_bandwidth":"1099256000000","last_bandwidth_update":"2018-08-09T13:23:15","average_market_bandwidth":3415484305,"lifetime_market_bandwidth":"237250000000","last_market_bandwidth_update":"2018-08-07T13:21:39","vesting_balance":"0.000 STEEM","reputation":"1564749115439","transfer_history":[],"market_history":[],"post_history":[],"vote_history":[],"other_history":[],"witness_votes":["guiltyparties"],"tags_usage":[],"guest_bloggers":[]},"status":"200"}null
Perhaps I have implemented this improperly?
for (int i =0 ;i < JA.length(); i++){
JSONObject JO = (JSONObject) JA.getJSONObject(i);
singleParsed = "User: " + JO.get("user.id") + "\n" +
"Location: " + JO.get("location") + "\n" +
"ID: " + JO.get("id")+"\n";
dataParsed = dataParsed + singleParsed;
}
UPDATED FIXES, STILL BROKEN BUT FARTHER ALONG.
String lines = "";
while(lines != null){
lines = bufferedReader.readLine();
data = data + lines;
}
JSONObject JO = new JSONObject(data);
String m = "";
for (int i =0 ;i < JO.length(); i++){
// JSONObject JO = (JSONObject) JO.getJSONObject(i);
singleParsed = "User: " + JO.getString("user.id") + "\n" +
"Location: " + JO.getString("location") + "\n" +
"ID: " + JO.getString("id")+"\n";
dataParsed = dataParsed + singleParsed;
DEBUGGER BREAKS ON "singleParsed = "user:", any ideas from here?
The response is a JSONObject not a JSONArray.
So, you can directly use: new JSONObject(data); in your code.
Also, as you haven't noticed, there's a null at the end after the closing brace.
I think you should parse the data with JSONObject, because the response is not an array. You should create class which contain User class and String for the status to handle the response.
Or you can use retrofit instead.
http://square.github.io/retrofit/
Try this...
try {
JSONObject object = new JSONObject(data);
String user = object.getString("user");
int id = user.getInt("id");
String name = user.getString("name");
String owner = user.getString("owner");
int weight_threshold = owner.getInt("weight_threshold");
JSONArray account_auths = owner.getJSONArray("account_auths");
.....
} catch (Exception e) {
e.printStackTrace();
}
pass other objects so on.
i'm stuck with a JSon problem, i'm trying to get a value contained in a JSon Object witch is itself Contained in another JSon Object. The returned JSon is like this " {"id":25,"name":"aaaaaaaa:eeeeegh","dishes_number":2,"description":"tttttttttttttf","country":{"code":"FR","name":"France"},"type":{"id":2,"name":"Main course"}} "
and i want to get the value od code in Country and the id in Type
here's my code
try{
JSONArray json = new JSONArray(sb.toString());
Courses coun;
for(int i=0; i < json.length(); i++) {
JSONObject jsonObject = json.getJSONObject(i);
coun = new Courses();
// Log.i(TAG, "Nom Pays : " + jsonObject.get("name"))
coun.setName((String) jsonObject.get("name"));
coun.setId((int) jsonObject.get("id"));
coun.setCountryCode((String) jsonObject.get("code"));
coun.setDescription((String) jsonObject.get("description"));
/* coun.setCourseTypeId((int) jsonObject.get("code"));
coun.setDishesNumber((int) jsonObject.get("code")); */
repas.add(coun);
}
}catch (JSONException je){
je.printStackTrace();
};
it give me the answer " org.json.JSONException: No value for code " when i run the app
Thanks you for your help
try{
JSONArray json = new JSONArray(sb.toString());
Courses coun;
for(int i=0; i < json.length(); i++) {
JSONObject jsonObject = json.getJSONObject(i);
coun = new Courses();
// Log.i(TAG, "Nom Pays : " + jsonObject.get("name"))
coun.setName((String) jsonObject.get("name"));
coun.setId((int) jsonObject.get("id"));
//get des country
JSONObject country = jsonObject.getJSONObject("country");
//get code and other informations of country
coun.setCountryCode(country.getString("code"));
coun.setDescription((String) jsonObject.get("description"));
/* coun.setCourseTypeId((int) jsonObject.get("code"));
coun.setDishesNumber((int) jsonObject.get("code")); */
repas.add(coun);
}
}catch (JSONException je){
je.printStackTrace();
};
Code field is nested in the country, so you need to get country first:
((JSONObject)jsonObject.get("country")).get("code")
The same way you can get type and nested id field from it.
P.S. Me test code:
public static void main(String[] args) {
String jsonStr = "[{\"id\":25,\"name\":\"aaaaaaaa:eeeeegh\",\"dishes_number\":2,\"description\":\"tttttttttttttf\",\"country\":{\"code\":\"FR\",\"name\":\"France\"},\"type\":{\"id\":2,\"name\":\"Main course\"}}]";
try {
JSONArray json = new JSONArray(jsonStr);
for (int i = 0; i < json.length(); i++) {
JSONObject jsonObject = json.getJSONObject(i);
System.out.println(((JSONObject)jsonObject.get("country")).get("code"));
}
} catch (JSONException je) {
je.printStackTrace();
}
}
Output:
FR
My output looks like this :
{
"IssueField1":{
"id":"customfield_10561",
"name":"Bug Disclaimer",
"type":null,
"value":"<div style>...</div>"
},
"IssueField2":{
"id":"customfield_13850",
"name":"ENV Work Type (DT)",
"type":null,
"value":null
},
.
.
.
"IssueField9":{
"id":"timespent",
"name":"Time Spent",
"type":"null",
"value":"null"
}
}
I want to create an ArrayList and and add all names in it if the value is not null. Any idea how should I do this in Java ?
Lets say you have the following json object:
{
"name":"john doe",
"age":100,
"job":"scientist",
"addresses":["address 1","address 2","address 3"]
}
to get the different fields inside of the object, create a JSONParser object and use the get() method to get the value held in that field
try {
FileReader reader = new FileReader("/path/to/file.json");
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
String name = (String) jsonObject.get("name");
System.out.println("The name is " + name);
long age = (long) jsonObject.get("age");
System.out.println("The age is: " + age);
JSONArray lang = (JSONArray) jsonObject.get("addresses");
for (int i = 0; i < lang.size(); i++) {
System.out.println("Address " + (i + 1) + ": " + lang.get(i));
}
} catch (FileNotFoundException fileNotFound) {
fileNotFound.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
} catch (NullPointerException npe) {
npe.printStackTrace();
} catch (org.json.simple.parser.ParseException e) {
e.printStackTrace();
}
}
i am tring to convert json data from string variable like this example :
String in = "{'employees': [{'firstName':'John' , 'lastName':'Doe' },"
+ "{ 'firstName' : 'Anna' , 'lastName' :'Smith' },"
+ "{ 'firstName' : 'Peter' , 'lastName' : 'Jones' }]}";
try {
String country = "";
JSONArray Array = new JSONArray(in);
for (int i = 0; i < Array.length(); i++) {
JSONObject sys = Array.getJSONObject(i);
country += " " + sys.getString("firstName");
}
Toast.makeText(this, country, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
};
when i try this code i get this error :
Error parsing data org.json.JSONException: Value 0 of type java.lang.Integer cannot be
converted to JSONObject
Actually your JSON is wrong formated.
Use string as below
String in = "{\"employees\": [{\"firstName\": \"John\",\"lastName\": \"Doe\"},{\"firstName\": \"Anna\",\"lastName\": \"Smith\"},{\"firstName\": \"Peter\",\"lastName\": \"Jones\"}]}";
try {
String country = "";
JSONObject jObj = new JSONObject(in);
JSONArray jArray = jObj.getJSONArray("employees");
for(int j=0; j <jArray.length(); j++){
JSONObject sys = jArray.getJSONObject(j);
country += " " + sys.getString("firstName");
}
Toast.makeText(this, country, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
Your String in is an object with key employees and value JSONArray.
So you need to parse in as a JSONObject and get the JSONArray employees from that object.
Try this
String in = "{'employees': [{'firstName':'John' , 'lastName':'Doe' },"
+ "{ 'firstName' : 'Anna' , 'lastName' :'Smith' },"
+ "{ 'firstName' : 'Peter' , 'lastName' : 'Jones' }]}";
try {
String country = "";
JSONObject jObj = new JSONObject(in);
JSONArray jArray = jObj.getJSONArray("employees");
for(int j=0; j <jArray.length(); j++){
JSONObject sys = jArray.getJSONObject(j);
country += " " + sys.getString("firstName");
}
Toast.makeText(this, country, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
try below code:-
JSONObejct j = new JSONObejct(in);
JSONArray Array = j.getJSONArray("employees");
note:-
{} denote JSONObject. ({employe})
[] denote JSONArray. (employee[])
Try to replace this line :
JSONArray Array = new JSONArray(in);
With this :
JSONObject json = new JSONObject(in);
JSONArray Array = new JSONArray(json.getJSONArray("employees"));