EDIT: I actually found the answer. I can't close the question as I am new. I was able to use Array.getString(i) to return the string value needed. Thanks for all the help.
编辑:我实际上找到了答案。因为我是新人,所以我无法解决这个问题。我能够使用Array.getString(i)返回所需的字符串值。谢谢你的帮助。
I have JSON like this:
我有这样的JSON:
{
"List": [
"example1",
"example2",
"example3",
"example4"
]
}
And I am trying to get the string value of those objects without using a key. How can I do that? The getString()
for jsonObject require a key and I don't have one.
我试图在不使用密钥的情况下获取这些对象的字符串值。我怎样才能做到这一点? jsonObject的getString()需要一个键,而我没有。
3 个解决方案
#1
7
I assume that you have a file :/home/user/file_001.json
我假设你有一个文件:/home/user/file_001.json
the file contains this : `
该文件包含:`
{"age":34,"name":"myName","messages":["msg 1","msg 2","msg 3"]}
Now let's write a program that reads the file : /home/user/file_001.json
and converts its content to a java JSONObject
.
现在让我们编写一个程序来读取文件:/home/user/file_001.json并将其内容转换为java JSONObject。
package org.xml.json;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JsonSimpleRead
{
@SuppressWarnings("unchecked")
public static void main(String[] args)
{
JSONParser parser = new JSONParser();
try
{
Object obj = parser.parse(new FileReader("/home/user/file_001.json"));
JSONObject jsonObject = (JSONObject) obj;
String name = (String) jsonObject.get("name");
System.out.println(name);
long age = (Long) jsonObject.get("age");
System.out.println(age);
JSONArray msg = (JSONArray) jsonObject.get("messages");
Iterator<String> iterator = msg.iterator();
while (iterator.hasNext())
{
System.out.println(iterator.next());
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ParseException e)
{
e.printStackTrace();
}
}
}
#2
0
"List" is your key because that is the property of the outermost JSON object:
“List”是您的密钥,因为它是最外层JSON对象的属性:
JSONObject json = new JSONObject( jsonString );
JSONArray array = json.getArray( "List" );
#3
0
Another solution:
import org.json.JSONArray;
import org.json.JSONObject;
// example json
String list = "{\"List\": [\"example1\", \"example2\", \"example3\", \"example4\"]}";
// to parse the keys & values of our list, we must turn our list into
// a json object.
JSONObject jsonObj = new JSONObject(list);
// isolate our list values by our key name (List)
String listValues = jsonObj.getString("List");
// turn our string of list values into a json array (to be parsed)
JSONArray listItems = new JSONArray(listValues);
// now we can print individual values using the getString and
// the index of the desired value (zero indexed)
Log.d("TAG", listItems.getString(2));
#1
7
I assume that you have a file :/home/user/file_001.json
我假设你有一个文件:/home/user/file_001.json
the file contains this : `
该文件包含:`
{"age":34,"name":"myName","messages":["msg 1","msg 2","msg 3"]}
Now let's write a program that reads the file : /home/user/file_001.json
and converts its content to a java JSONObject
.
现在让我们编写一个程序来读取文件:/home/user/file_001.json并将其内容转换为java JSONObject。
package org.xml.json;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JsonSimpleRead
{
@SuppressWarnings("unchecked")
public static void main(String[] args)
{
JSONParser parser = new JSONParser();
try
{
Object obj = parser.parse(new FileReader("/home/user/file_001.json"));
JSONObject jsonObject = (JSONObject) obj;
String name = (String) jsonObject.get("name");
System.out.println(name);
long age = (Long) jsonObject.get("age");
System.out.println(age);
JSONArray msg = (JSONArray) jsonObject.get("messages");
Iterator<String> iterator = msg.iterator();
while (iterator.hasNext())
{
System.out.println(iterator.next());
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ParseException e)
{
e.printStackTrace();
}
}
}
#2
0
"List" is your key because that is the property of the outermost JSON object:
“List”是您的密钥,因为它是最外层JSON对象的属性:
JSONObject json = new JSONObject( jsonString );
JSONArray array = json.getArray( "List" );
#3
0
Another solution:
import org.json.JSONArray;
import org.json.JSONObject;
// example json
String list = "{\"List\": [\"example1\", \"example2\", \"example3\", \"example4\"]}";
// to parse the keys & values of our list, we must turn our list into
// a json object.
JSONObject jsonObj = new JSONObject(list);
// isolate our list values by our key name (List)
String listValues = jsonObj.getString("List");
// turn our string of list values into a json array (to be parsed)
JSONArray listItems = new JSONArray(listValues);
// now we can print individual values using the getString and
// the index of the desired value (zero indexed)
Log.d("TAG", listItems.getString(2));