0

here's my problem. I'm getting a random json file form the internet like this one or this one.

I'm trying to parse it with JSON in android studio (using for example, gson). But I can't find such an option in gson that lets me select a token from the JSON file without knowing the JSON structure (and creating a class and that stuff). When I tried to do this in VisualBasic.NET it was really easy, using this code and the NewtonSoft.Json library:

 Dim jsonSet As JObject = JObject.Parse(responseFromServer)
    balance = jsonSet.SelectToken("$..balance")

But it seems way harder to do this in Java... can someone help me?

glennsl
  • 28,186
  • 12
  • 57
  • 75
Xamortex
  • 1
  • 4
  • Possible duplicate of [How can I convert JSON to a HashMap using Gson?](https://stackoverflow.com/questions/2779251/how-can-i-convert-json-to-a-hashmap-using-gson) – logcat Oct 16 '17 at 18:50

3 Answers3

0

Gson is an object serialization/deserialization library. Its purpose is to serialize to and from known objects.

You want to use a more basic library of which there are more than a few implementations available. Some of which are listed http://www.json.org/

They allow you to write code like

JSONObject obj = new JSONObject("{}");
Deadron
  • 5,135
  • 1
  • 16
  • 27
  • Yes, the problem is I didn't find any library to do the option I'm asking for. Gson was just an example. Do you know of any library that would work like NewtonSoft.Json did? – Xamortex Oct 16 '17 at 19:14
0

You can paste your json String to http://www.jsonschema2pojo.org/

it will create object of jsonString.

Example with your json:

    -----------------------------------com.example.Datum.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Datum {

@SerializedName("address")
@Expose
private String address;
@SerializedName("balance")
@Expose
private Integer balance;
@SerializedName("nonce")
@Expose
private Object nonce;
@SerializedName("code")
@Expose
private String code;
@SerializedName("name")
@Expose
private Object name;
@SerializedName("storage")
@Expose
private Object storage;
@SerializedName("firstSeen")
@Expose
private String firstSeen;

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public Integer getBalance() {
return balance;
}

public void setBalance(Integer balance) {
this.balance = balance;
}

public Object getNonce() {
return nonce;
}

public void setNonce(Object nonce) {
this.nonce = nonce;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public Object getName() {
return name;
}

public void setName(Object name) {
this.name = name;
}

public Object getStorage() {
return storage;
}

public void setStorage(Object storage) {
this.storage = storage;
}

public String getFirstSeen() {
return firstSeen;
}

public void setFirstSeen(String firstSeen) {
this.firstSeen = firstSeen;
}

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("status")
@Expose
private Integer status;
@SerializedName("data")
@Expose
private List<Datum> data = null;

public Integer getStatus() {
return status;
}

public void setStatus(Integer status) {
this.status = status;
}

public List<Datum> getData() {
return data;
}

public void setData(List<Datum> data) {
this.data = data;
}

}

After you will get object from jsonString with function:

// Deserialize to single object.
    public Example deserializeFromJson(String jsonString) {
        Gson gson = new Gson();
        Example myClass = gson.fromJson(jsonString, Example.class);
        return myClass;
    }

And you can get everything in your object.

I hope it can help your problem!

Thientvse
  • 1,753
  • 1
  • 14
  • 23
  • With this i would still have the same problem I had. I need one code that works for more than one non-generic json api. What you propose is to create one class for each JSON file I have, but I'm asking for something more simpe: given a JSON file, get the value of a token no matter what the structure of the file is, even if the token is inside an array. With the code I pasted for NewtonSoft.Json using .NET it worked plain and simple. Thank you anyways for your help! – Xamortex Oct 17 '17 at 06:47
0

Well, finally after doing some more research I found this: https://github.com/json-path/JsonPath

That's exactly what I needed, as it lets you access a JSON Object and finding a token with its path, exactly as I did with NewtonSoft.Json in .NET.

I think it's really interesting, as no matter what the structure of the JSON file is, you can find a value by giving only the path with the format "$..path".

Xamortex
  • 1
  • 4