2017-10-16 51 views
0

这是我的问题。我从互联网上得到一个随机的json文件,如this onethis one在不知道JSON格式的情况下解析JSON(与selectToken一样)

我想解析它与JSON在android studio(使用例如,gson)。但是我无法在gson中找到这样的选项,让我从JSON文件中选择一个不知道JSON结构的标记(并创建一个类和那些东西)。当我试图做到这一点在VisualBasic.NET它是很容易,使用此代码和NewtonSoft.Json库:

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

但似乎更难的方式做到这一点在Java中...有人可以帮助我吗?

+0

可能重复[如何使用Gson将JSON转换为HashMap?](https://stackoverflow.com/questions/2779251/how-can-i-convert-json-to-a-hashmap-using- gson) – logcat

回答

0

好了,终于做一些更多的研究后,我发现这一点: https://github.com/json-path/JsonPath

这正是我需要的,因为它可以让你访问一个JSON对象,并找到其路径的道理,正是因为我NewtonSoft做.NET中的Json。

我认为这真的很有趣,因为不管JSON文件的结构如何,只要给出格式为“$ .. path”的路径即可找到值。

0

Gson是一个对象序列化/反序列化库。其目的是序列化已知对象和已知对象。

你想使用一个更基本的库,其中有几个可用的实现。其中一些上市http://www.json.org/

它们允许你写代码像

JSONObject obj = new JSONObject("{}"); 
+0

是的,问题是我没有找到任何库来做我要求的选项。 Gson只是一个例子。你知道任何像NewtonSoft.Json一样工作的图书馆吗? – Xamortex

+0

https://mvnrepository.com/artifact/org.json/json – Deadron

0

您可以将您的JSON字符串粘贴到http://www.jsonschema2pojo.org/

它会创建jsonString的对象。

实例与您的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; 
} 

} 

后,你会得到对象从jsonString与功能:

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

,你可以得到你的对象的一切。

我希望它能帮助你的问题!

+0

有了这个我仍然会遇到同样的问题。我需要一个适用于多个非泛型json api的代码。你的建议是为每个JSON文件创建一个类,但我要求更多的类似:给定一个JSON文件,无论文件的结构如何,都可以获取令牌的值,即使令牌在一个数组中。用我使用.NET粘贴NewtonSoft.Json的代码,它工作简单而且简单。非常感谢您的帮助! – Xamortex

相关问题