I want to parse a JSON object from an endpoint (this one here: https://api.coinmarketcap.com/v1/ticker/bitcoin/) and store the value in a variable at a specific attribute, which in this case is the name.
This the ERROR i get:
java.lang.IllegalStateException: Expected a name but was STRING...
AsyncTask.execute(new Runnable() {
#Override
public void run() {
// All your networking logic
// should be here
try {
String u = "https://api.coinmarketcap.com/v1/ticker/bitcoin";
URL coinMarketCapApi = new URL(u);
HttpsURLConnection myConnection = (HttpsURLConnection) coinMarketCapApi.openConnection();
myConnection.setRequestProperty("User-Agent", "my-rest-app-v0.1");
if (myConnection.getResponseCode() == 200) {
// Success
InputStream responseBody = myConnection.getInputStream();
InputStreamReader responseBodyReader =
new InputStreamReader(responseBody, "UTF-8");
JsonReader jsonReader = new JsonReader(responseBodyReader);
jsonReader.beginArray();
while (jsonReader.hasNext()) {
String key = jsonReader.nextName();
if (key.equals("name")) {
String value = jsonReader.nextName();
break; // Break out of the loop
} else {
jsonReader.skipValue();
}
}
jsonReader.close();
myConnection.disconnect();
} else {
// Error handling code goes here
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
you can convert the InputStream to String and then Create JSONArray from that string. like
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, encoding);
String theString = writer.toString();
JSONArray jsonarray = new JSONArray(theString);
This way you don't have to manually construct the array.
Use this depandency for JSONArray
https://mvnrepository.com/artifact/org.json/json
You can fix the problem using gson.
https://github.com/google/gson
com.google.gson.stream.JsonReader jsonReader =
new com.google.gson.stream.JsonReader(new InputStreamReader(responseBody));
ArrayList<Coin> coins = new Gson().fromJson(jsonReader, Coin.class);
coins.forEach(coin -> System.out.println(coin.name));
public class Coin{
private String id;
private String name;
private String symbol;
private int rank;
#SerializedName("price_usd")
private double priceUsd;
...........
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getSymbol() {
return symbol;
}
public int getRank() {
return rank;
}
public double getPriceUsd() {
return priceUsd;
}
..........
}
Related
I really need help here, im Deserializing the next json:
{
"name":"myname",
"userID":"12345",
"password":"sha1passwordASDESFSGSD",
"active":"1",
"profile":"2",
"job":"Manager"
}
Im using Jersey to create webService, when i recive the json i recive it as a InputStream
Also i tried with a String
#POST
#Path("user/in/")
#Produces("application/json")
public String InsertUser(InputStream inStr)
{
String line = null, res = "POST 200 > ";
BufferedReader br = new BufferedReader(new InputStreamReader(inStr));
try {
while ((line = br.readLine()) != null) {
System.out.println(line);
res += line;
}
} catch (IOException e) {
e.printStackTrace();
}
try {
UserInformation user = new Gson().fromJson(res, UserInformation.class);
System.out.println("All done");
System.out.println(user.getName());
} catch (Exception e) {
System.out.println("Error al convertir jo object " + e.getCause() + e.getMessage());
}
return "POST 200 > ";
}
I tried using a InputStreamReader:
#POST
#Path("user/in/")
#Produces("application/json")
public String InsertUser(InputStream inStr)
{
try {
InputStreamReader isr = new InputStreamReader(inStr);
UserInformation user = new Gson().fromJson(isr, UserInformation.class);
System.out.println("All done");
System.out.println(user.getName());
} catch (Exception e) {
System.out.println("Error al convertir jo object " + e.getCause() + e.getMessage());
}
return "POST 200 > ";
}
Neither of those codes work. They don't throw an exception or print "All done".
When i debug the object, user doesn't appear in the variables menu.
In my experience, is because an error is happening in the line UserInformation user = new Gson().fromJson(isr, UserInformation.class);
But i cant see which one is.
My UserInformation Class is the next
public class UserInformation {
private String name;
private String userID;
private String password;
private String active;
private String profile;
private String job;
// Methods removed for brevity
}
I am assuming you are using Google gson. Here is my answer:
#Post
#Path("user/in")
#Produces(MediaType.APPLICATION_JSON)
public Response InsertUser(string json){
JsonElement jsonElement = new JsonParser().parse(json);
JsonObject object = jsonElement.getAsJsonObject();
String name = object.getAsJsonPrimitive("name").getAsString();
int userID = object.getAsJsonPrimitve("userID").getAsInt();
String password = object.getAsJsonPrimitive("password").getAsString();
String job = object.getAsJsonPrimitive("job").getAsString();
int active = object.getAsJsonPrimitve("active").getAsInt();
int profile = object.getAsJsonPrimitve("profile").getAsInt();
return Response.status(Response.Status.OK).entity("all done").build();
}
I already find the solution, the problem was that i was receiving a InputStream,
following its recommendations the solution code is:
#POST
#Path("users/in/")
#Produces(MediaType.APPLICATION_JSON)
public String InsertUser(String json)
{
try{
UserInformation user = new Gson().fromJson(json, UserInformation.class);
String name = user.getName();
String userID = user.getUserID();
String password = user.getPassword();
String job = user.getJob();
String active = user.getActive();
String profile = user.getProfile();
}catch(Exception e){
System.out.println(Response.status(500).entity(e.getCause() + e.getMessage()).build().toString());
return Response.status(500).entity(e.getCause() + e.getMessage()).build().toString();
}
}
I have not seen an (answered) example on the web which discusses this kind of nested-json-array.
JSON to be parsed:
{
"Field": {
"ObjectsList": [
{
"type": "Num",
"priority": "Low",
"size": 3.43
},
{
"type": "Str",
"priority": "Med",
"size": 2.61
}
]
}
}
I created a class for each 'level' of nested json block. I want to be able to parse the contents of the "ObjectList" array.
Can anyone help me to parse this JSON using Gson in Java?
Any hints or code-snippets would be greatly appreciated.
My approach is the following:
public static void main (String... args) throws Exception
{
URL jsonUrl = new URL("http://jsonUrl.com") // cannot share the url
try (InputStream input = jsonUrl.openStream();
BufferedReader buffReader = new BufferedReader (new InputStreamReader (input, "UTF-8")))
{
Gson gson = new GsonBuilder().create();
ClassA classA = gson.fromJson(buffReader, ClassA.class);
System.out.println(classA);
}
}
}
class ClassA
{
private String field;
// getter & setter //
}
class ClassB
{
private List<ClassC> objList;
// getter & setter //
}
clas ClassC
{
private String type;
private String priority;
private double size;
// getters & setters //
public String printStr()
{
return String.format(type, priority, size);
}
}
The following snippet and source file would help you:
https://github.com/matpalm/common-crawl-quick-hacks/blob/master/links_in_metadata/src/com/matpalm/MetaDataToTldLinks.java#L17
private static ParseResult NO_LINKS = new ParseResult(new HashSet<String>(), 0);
private JsonParser parser;
public static void main(String[] s) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(s[0]));
MetaDataToTldLinks metaDataToTldLinks = new MetaDataToTldLinks();
while (reader.ready()) {
String[] fields = reader.readLine().split("\t");
ParseResult outboundLinks = metaDataToTldLinks.outboundLinks(fields[1]);
System.out.println(tldOf(fields[0]) + " " + outboundLinks.links);
}
}
public MetaDataToTldLinks() {
this.parser = new JsonParser();
}
public ParseResult outboundLinks(String jsonMetaData) {
JsonObject metaData = parser.parse(jsonMetaData.toString()).getAsJsonObject();
if (!"SUCCESS".equals(metaData.get("disposition").getAsString()))
return NO_LINKS;
JsonElement content = metaData.get("content");
if (content == null)
return NO_LINKS;
JsonArray links = content.getAsJsonObject().getAsJsonArray("links");
if (links == null)
return NO_LINKS;
Set<String> outboundLinks = new HashSet<String>();
int numNull = 0;
for (JsonElement linke : links) {
JsonObject link = linke.getAsJsonObject();
if ("a".equals(link.get("type").getAsString())) { // anchor
String tld = tldOf(link.get("href").getAsString());
if (tld == null)
++numNull;
else
outboundLinks.add(tld);
}
}
return new ParseResult(outboundLinks, numNull);
}
public static String tldOf(String url) {
try {
String tld = new URI(url).getHost();
if (tld==null)
return null;
if (tld.startsWith("www."))
tld = tld.substring(4);
tld = tld.trim();
return tld.length()==0 ? null : tld;
}
catch (URISyntaxException e) {
return null;
}
}
public static class ParseResult {
public final Set<String> links;
public final int numNull;
public ParseResult(Set<String> links, int numNull) {
this.links = links;
this.numNull = numNull;
}
}
How about this snippet?:
if (json.isJsonArray()) {
JsonArray array = json.getAsJsonArray();
List<Object> out = Lists.newArrayListWithCapacity(array.size());
for (JsonElement item : array) {
out.add(toRawTypes(item));
}
}
My server responds with JSON object always containing the same structre:
[int id, Error error, T result] where result is a type depending on the JSON method being called.
I'm trying to use GSON for deserialization but it looks like having problems when I use generics.
private <T> T executeRequest(String methodName, Object[] params,
Class<T> resultType) throws HttpException, IOException {
final Gson gson = buildGson();
final URL url = new URL(WEB_URL + methodName);
final ZenfolioRequest request = new ZenfolioRequest();
request.setMethod(methodName);
request.setId(getNextRequestId());
request.setParams(params);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(POST);
connection.setRequestProperty(USER_AGENT_PROPERTY, AGENT_NAME);
connection.setRequestProperty(CONTENT_TYPE, APPLICATION_JSON);
connection.setRequestProperty(ACCEPT, APPLICATION_JSON);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setChunkedStreamingMode(0);
connection.connect();
final String toSend = gson.toJson(request);
connection.getOutputStream().write(toSend.getBytes());
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
final InputStream in = new BufferedInputStream(
connection.getInputStream());
//String response = readResponseString(in);
final T result = gson.fromJson(new InputStreamReader(in, UTF_8),
resultType);
return result;
} else {
throw new HttpException();
}
}
Example method:
#Override
public ZenfolioResponse<AuthChallenge> getChallenge(String loginName) {
try {
final String methodName = "GetChallenge";
return executeRequest(methodName, new Object[] { loginName },
new ZenfolioResponse<AuthChallenge>().getClass());
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
return null;
}
public class ZenfolioResponse<T>{
#SerializedName("id")
int id;
#SerializedName("error")
Error error;
#SerializedName("result")
T result;
public T getResult() {
return result;
}
public void setResult(T result) {
this.result = result;
}
public Error getError() {
return error;
}
public void setError(Error error) {
this.error = error;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
The problem is the object ZenfolioResponse is incorrectly filled with the Result body. It is not typed, it is an Object instance instead of AuthChallenge type.
Does anybody have any hints on how to tweak this?
Try to use TypeToken from GSON library. It will help you to parse generic type. I found example here:
http://www.studytrails.com/java/json/java-google-json-serializing-classes-with-generic-type.jsp
I'm fairly new to android development and I am trying to create an android application for currency conversion. I need to read JSON URL to get the rate. I get user input for AMOUNT, CURRENCYFROM and CURRENCYTO. The url I'm trying to read is the following: http://rate-exchange.appspot.com/currency?from=USD&to=EUR&q=1
Once url read I want to assign the values to variables. Specifically I'm trying to get the rate and multiply by user input for AMOUNT.
I want to get the Dictionaries(I think the JSON file has dictionaries) value and keys and convert it to data types variables such as int or string. Any help would be appreciated. Thanks
You can use this code :
public API_Rate_Model getRate() {
API_Rate_Model result = new API_Rate_Model();
try {
HttpResponse response;
HttpClient myClient = new DefaultHttpClient();
HttpPost myConnection = new HttpPost("http://rate-exchange.appspot.com/currency?from=USD&to=EUR&q=1");
try {
response = myClient.execute(myConnection);
String JSONString = EntityUtils.toString(response.getEntity(),
"UTF-8");
Log.i(BaseID.TAG, JSONString);
JSONObject json = null;
json = new JSONObject(JSONString);
result.setTo(json.getString("to"));
result.setRate(json.getDouble("rate"));
result.setFrom(json.getString("from"));
result.setValue(json.getDouble("v"));
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
API_Rate_Model Class :
public class API_Rate_Model {
private String to;
private Double rate;
private String from;
private Double value;
public API_001_Model() {
to = "";
rate = 0.0;
from = "";
value = 0.0;
}
public Double getRate() {
return rate;
}
public void setRate(Double rate) {
this.rate = rate;
}
public int getValue() {
return value;
}
public void setValue(Double value) {
this.value = value;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to= to;
}
}
this code will sent a post to the server then convert the response to string then process the JSON String response. I hope my answer can helps you, but if you have any question feel free to ask in the comment :)
Here is my Json returned from the server
{"ErrorCode":1005,"Message":"Username does not exist"}
Here is my class for an error
public class ErrorModel {
public int ErrorCode;
public String Message;
}
and here is my conversion code.
public static ErrorModel GetError(String json) {
Gson gson = new Gson();
try
{
ErrorModel err = gson.fromJson(json, ErrorModel.class);
return err;
}
catch(JsonSyntaxException ex)
{
return null;
}
}
It is always throwing a JsonSyntaxException. Any ideas what could be my problem here?
EDIT: As requested, here is further elaboration.
My backend is an ASP.NET MVC 2 application acting as a rest API. The backend isn't the problem here, as my actions (and even server errors) all return Json (using the built in JsonResult). Here's a sample.
[HttpPost]
public JsonResult Authenticate(AuthenticateRequest request)
{
var authResult = mobileService.Authenticate(request.Username, request.Password, request.AdminPassword);
switch (authResult.Result)
{
//logic omitted for clarity
default:
return ExceptionResult(ErrorCode.InvalidCredentials, "Invalid username/password");
break;
}
var user = authResult.User;
string token = SessionHelper.GenerateToken(user.UserId, user.Username);
var result = new AuthenticateResult()
{
Token = token
};
return Json(result, JsonRequestBehavior.DenyGet);
}
The basic logic is to auth the user cretentials and either return an ExceptionModel as json or an AuthenticationResult as json.
Here is my server side Exception Model
public class ExceptionModel
{
public int ErrorCode { get; set; }
public string Message { get; set; }
public ExceptionModel() : this(null)
{
}
public ExceptionModel(Exception exception)
{
ErrorCode = 500;
Message = "An unknown error ocurred";
if (exception != null)
{
if (exception is HttpException)
ErrorCode = ((HttpException)exception).ErrorCode;
Message = exception.Message;
}
}
public ExceptionModel(int errorCode, string message)
{
ErrorCode = errorCode;
Message = message;
}
}
When the above authentication is called with invalid credentials, the error result is returned as expected. The Json returned is the Json above in the question.
On the android side, I first build an object with my key-value pairs.
public static HashMap<String, String> GetAuthenticationModel(String username, String password, String adminPassword, String abbr)
{
HashMap<String, String> request = new HashMap<String, String>();
request.put("SiteAbbreviation", abbr);
request.put("Username", username);
request.put("Password", password);
request.put("AdminPassword", adminPassword);
return request;
}
Then, I send off an http post and return as a string whatever is sent back.
public static String Post(ServiceAction action, Map<String, String> values) throws IOException {
String serviceUrl = GetServiceUrl(action);
URL url = new URL(serviceUrl);
URLConnection connection = url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String data = GetPairsAsString(values);
DataOutputStream output = new DataOutputStream(connection.getOutputStream());
output.writeBytes(data);
output.flush();
output.close();
DataInputStream input = new DataInputStream(connection.getInputStream());
String line;
String result = "";
while (null != ((line = input.readLine())))
{
result += line;
}
input.close ();
return result;
}
private static String GetServiceUrl(ServiceAction action)
{
return "http://192.168.1.5:33333" + action.toString();
}
private static String GetPairsAsString(Map<String, String> values){
String result = "";
Iterator<Entry<String, String>> iter = values.entrySet().iterator();
while(iter.hasNext()){
Map.Entry<String, String> pairs = (Map.Entry<String, String>)iter.next();
result += "&" + pairs.getKey() + "=" + pairs.getValue();
}
//remove the first &
return result.substring(1);
}
Then I take that result and pass it into my parser to see if it is an error
public static ErrorModel GetError(String json) {
Gson gson = new Gson();
try
{
ErrorModel err = gson.fromJson(json, ErrorModel.class);
return err;
}
catch(JsonSyntaxException ex)
{
return null;
}
}
But, JsonSyntaxException is always thrown.
Might help to know more about the exception, but the same code sample works fine here. I suspect there's a piece of code you omitted that's causing the problem (perhaps the creation/retrieval of the JSON string). Here's a code sample that worked fine for me on Java 1.6 and Gson 1.6:
import com.google.gson.Gson;
public class ErrorModel {
public int ErrorCode;
public String Message;
public static void main(String[] args) {
String json = "{\"ErrorCode\":1005,\"Message\":\"Username does not exist\"}";
Gson gson = new Gson();
ErrorModel err = gson.fromJson(json, ErrorModel.class);
System.out.println(err.ErrorCode);
System.out.println(err.Message);
}
}