This is my JSON data.
这是我的JSON数据。
{"JSONDATA":[{"key":0,"value":"--Any--"},{"key":61,"value":"Accounting"},{"key":81,"value":"Aerospace & Defense"},{"key":72,"value":"Automotive"},{"key":83,"value":"Banking"},{"key":84,"value":"Biotech"},{"key":85,"value":"Construction"},{"key":86,"value":"Customer Service"},{"key":87,"value":"Education"},{"key":82,"value":"Energy"},{"key":70,"value":"Finance"},{"key":193,"value":"Government"},{"key":194,"value":"Healthcare"},{"key":71,"value":"Insurance"},{"key":73,"value":"Legal"},{"key":62,"value":"Management"},{"key":63,"value":"Manufacturing"},{"key":64,"value":"Marketing\/Advertising"},{"key":77,"value":"Media - Journalism"},{"key":74,"value":"Pharmaceutical"},{"key":75,"value":"Real Estate"},{"key":76,"value":"Research"},{"key":65,"value":"Restaurant"},{"key":66,"value":"Retail"},{"key":67,"value":"Sales"},{"key":78,"value":"Science"},{"key":68,"value":"Telecommunications"},{"key":79,"value":"Training"},{"key":69,"value":"Transportation"},{"key":80,"value":"Utilities"}]}
I want to decode it on my Android App, This is the code i have used., But i don't get anything on my output. No errors too.
我想在我的Android应用程序上解码它,这是我用过的代码。但是我的输出没有任何结果。没有错误。
JSONObject jObject= new JSONObject();
JSONArray menuObject = new JSONArray(jObject.getString("JSONDATA"));
String app;
for (int i = 0; i<menuObject.length(); i++) {
{
app=menuObject.getJSONObject(i).getString("value").toString();
a.append(app); // a is my TextView
}
6 个解决方案
#1
1
Its wrong:
JSONArray menuObject = new JSONArray(jObject.getString("JSONDATA"));
Try:
JSONObject jObject= new JSONObject(yourJSONString);
JSONArray menuObject = jObject.getJSONArray("JSONDATA");
Keep one thing in mind: Create a JSON Object with JSON String you want to parse and then you can fetch String/JSON Object or JSON Array from the created JSON Object.
记住一件事:使用要解析的JSON String创建一个JSON对象,然后可以从创建的JSON对象中获取String / JSON对象或JSON数组。
#2
4
First off, you're not initializing your jObject
with anything.
首先,你没有用任何东西初始化你的jObject。
//pass in string
JSONObject jObject= new JSONObject(jsonString);
JSONObject
s need something to parse, otherwise (the way you have it now) they initialize with no data, which isn't very helpful.
JSONObjects需要解析一些东西,否则(你现在的方式)他们初始化没有数据,这不是很有帮助。
Secondly, you're using getString
when you really want an array:
其次,当你真正想要一个数组时,你正在使用getString:
JSONArray menuObject = jObject.getJSONArray("JSONDATA");
getString
is designed to return a piece of string data from a JSON object. "JSONDATA"
holds an array, so we need to choose the correct type to retrieve.
getString旨在从JSON对象返回一段字符串数据。 “JSONDATA”包含一个数组,因此我们需要选择要检索的正确类型。
Thirdly, you have a redundant toString()
, as getString
already returns a String
:
第三,你有一个冗余的toString(),因为getString已经返回一个String:
app=menuObject.getJSONObject(i).getString("value");
#3
0
Store your json response in a String
将json响应存储在String中
String jsonResponse="YOUR JSON RESPONSE STRING";
//Pass the string as below
//传递字符串如下
JSONObject jObject= new JSONObject(jsonResponse);
JSONArray menuObject = jObject.getJSONArray("JSONDATA"));
String app;
for (int i = 0; i<menuObject.length(); i++) {
{
app=menuObject.getJSONObject(i).getString("value").toString();
a.append(app); // a is my TextView
}
#4
0
Use the appropiate getters and setters in JSONObject
and JSONArray
, and your "JSONDATA" entry is not a string. Do something like this:
在JSONObject和JSONArray中使用适当的getter和setter,并且“JSONDATA”条目不是字符串。做这样的事情:
JSONObject jObject = new JSONObject(yourJsonString);
JSONArray menuArray = jObject.getJSONArray("JSONDATA");
for (int i = 0; i < menuArray.length(); i++) {
String app = menuObject.getJSONObject(i).getString("value");
a.append(app); // a is my TextView
}
#5
0
Use following code for parse your json string.
使用以下代码解析您的json字符串。
JSONObject obj = new JSONObject(youtString);
JSONArray array = obj.getJSONArray("JSONDATA");
for (int i = 0; i < array.length(); i++) {
JSONObject c = array.getJSONObject(i);
String key = c.getString("key");
String value = c.getString("value");
a.append(value);
}
#6
0
Use this:-
String result="[{"key":0,"value":"--Any--"},{"key":61,"value":"Accounting"},{"key":81,"value":"Aerospace & Defense"},{"key":72,"value":"Automotive"},{"key":83,"value":"Banking"},{"key":84,"value":"Biotech"},{"key":85,"value":"Construction"},{"key":86,"value":"Customer Service"},{"key":87,"value":"Education"},{"key":82,"value":"Energy"},{"key":70,"value":"Finance"},{"key":193,"value":"Government"},{"key":194,"value":"Healthcare"},{"key":71,"value":"Insurance"},{"key":73,"value":"Legal"},{"key":62,"value":"Management"},{"key":63,"value":"Manufacturing"},{"key":64,"value":"Marketing\/Advertising"},{"key":77,"value":"Media - Journalism"},{"key":74,"value":"Pharmaceutical"},{"key":75,"value":"Real Estate"},{"key":76,"value":"Research"},{"key":65,"value":"Restaurant"},{"key":66,"value":"Retail"},{"key":67,"value":"Sales"},{"key":78,"value":"Science"},{"key":68,"value":"Telecommunications"},{"key":79,"value":"Training"},{"key":69,"value":"Transportation"},{"key":80,"value":"Utilities"}]";
JSONArray menuObject = new JSONArray(result);
String app;
for (int i = 0; i<menuObject.length(); i++) {
{
app=menuObject.getJSONObject(i).getString("value").toString();
a.append(app); // a is my TextView
}
#1
1
Its wrong:
JSONArray menuObject = new JSONArray(jObject.getString("JSONDATA"));
Try:
JSONObject jObject= new JSONObject(yourJSONString);
JSONArray menuObject = jObject.getJSONArray("JSONDATA");
Keep one thing in mind: Create a JSON Object with JSON String you want to parse and then you can fetch String/JSON Object or JSON Array from the created JSON Object.
记住一件事:使用要解析的JSON String创建一个JSON对象,然后可以从创建的JSON对象中获取String / JSON对象或JSON数组。
#2
4
First off, you're not initializing your jObject
with anything.
首先,你没有用任何东西初始化你的jObject。
//pass in string
JSONObject jObject= new JSONObject(jsonString);
JSONObject
s need something to parse, otherwise (the way you have it now) they initialize with no data, which isn't very helpful.
JSONObjects需要解析一些东西,否则(你现在的方式)他们初始化没有数据,这不是很有帮助。
Secondly, you're using getString
when you really want an array:
其次,当你真正想要一个数组时,你正在使用getString:
JSONArray menuObject = jObject.getJSONArray("JSONDATA");
getString
is designed to return a piece of string data from a JSON object. "JSONDATA"
holds an array, so we need to choose the correct type to retrieve.
getString旨在从JSON对象返回一段字符串数据。 “JSONDATA”包含一个数组,因此我们需要选择要检索的正确类型。
Thirdly, you have a redundant toString()
, as getString
already returns a String
:
第三,你有一个冗余的toString(),因为getString已经返回一个String:
app=menuObject.getJSONObject(i).getString("value");
#3
0
Store your json response in a String
将json响应存储在String中
String jsonResponse="YOUR JSON RESPONSE STRING";
//Pass the string as below
//传递字符串如下
JSONObject jObject= new JSONObject(jsonResponse);
JSONArray menuObject = jObject.getJSONArray("JSONDATA"));
String app;
for (int i = 0; i<menuObject.length(); i++) {
{
app=menuObject.getJSONObject(i).getString("value").toString();
a.append(app); // a is my TextView
}
#4
0
Use the appropiate getters and setters in JSONObject
and JSONArray
, and your "JSONDATA" entry is not a string. Do something like this:
在JSONObject和JSONArray中使用适当的getter和setter,并且“JSONDATA”条目不是字符串。做这样的事情:
JSONObject jObject = new JSONObject(yourJsonString);
JSONArray menuArray = jObject.getJSONArray("JSONDATA");
for (int i = 0; i < menuArray.length(); i++) {
String app = menuObject.getJSONObject(i).getString("value");
a.append(app); // a is my TextView
}
#5
0
Use following code for parse your json string.
使用以下代码解析您的json字符串。
JSONObject obj = new JSONObject(youtString);
JSONArray array = obj.getJSONArray("JSONDATA");
for (int i = 0; i < array.length(); i++) {
JSONObject c = array.getJSONObject(i);
String key = c.getString("key");
String value = c.getString("value");
a.append(value);
}
#6
0
Use this:-
String result="[{"key":0,"value":"--Any--"},{"key":61,"value":"Accounting"},{"key":81,"value":"Aerospace & Defense"},{"key":72,"value":"Automotive"},{"key":83,"value":"Banking"},{"key":84,"value":"Biotech"},{"key":85,"value":"Construction"},{"key":86,"value":"Customer Service"},{"key":87,"value":"Education"},{"key":82,"value":"Energy"},{"key":70,"value":"Finance"},{"key":193,"value":"Government"},{"key":194,"value":"Healthcare"},{"key":71,"value":"Insurance"},{"key":73,"value":"Legal"},{"key":62,"value":"Management"},{"key":63,"value":"Manufacturing"},{"key":64,"value":"Marketing\/Advertising"},{"key":77,"value":"Media - Journalism"},{"key":74,"value":"Pharmaceutical"},{"key":75,"value":"Real Estate"},{"key":76,"value":"Research"},{"key":65,"value":"Restaurant"},{"key":66,"value":"Retail"},{"key":67,"value":"Sales"},{"key":78,"value":"Science"},{"key":68,"value":"Telecommunications"},{"key":79,"value":"Training"},{"key":69,"value":"Transportation"},{"key":80,"value":"Utilities"}]";
JSONArray menuObject = new JSONArray(result);
String app;
for (int i = 0; i<menuObject.length(); i++) {
{
app=menuObject.getJSONObject(i).getString("value").toString();
a.append(app); // a is my TextView
}