将JSON字符串中的值转换为对象。

huangapple go评论112阅读模式
英文:

Convert values from json string to object

问题

I have a JSON payload of the format, with each value a list with a single map element:

  1. {
  2. "redundant_str_1": [
  3. {
  4. "attr_1": "val1",
  5. "attr_2": "val2"
  6. }
  7. ],
  8. "rendundant_str_2": [
  9. {
  10. "attr_1": "val4",
  11. "attr_2": "val3"
  12. }
  13. ]
  14. }

Model is:

  1. public class MyObj {
  2. private String attr_1;
  3. private String attr_2;
  4. }

How can I map the above response to List<MyObj> by neglecting the keys and taking only elements from lists?

英文:

I have a JSON payload of the format, with each value a list with single map element:

  1. {
  2. &quot;redundant_str_1&quot;: [
  3. {
  4. &quot;attr_1&quot;: &quot;val1&quot;,
  5. &quot;attr_2&quot;: &quot;val2&quot;
  6. }
  7. ],
  8. &quot;rendundant_str_2&quot;: [
  9. {
  10. &quot;attr_1&quot;: &quot;val4&quot;,
  11. &quot;attr_2&quot;: &quot;val3&quot;
  12. }
  13. ]
  14. }

Model is:

  1. public class MyObj {
  2. private String attr_1;
  3. private String attr_2;
  4. }

How can I map above response to List&lt;MyObj&gt; by neglecting the keys and taking only element from lists?

答案1

得分: 1

以下是代码的翻译部分:

  1. String json = "{\"redundant_str_1\": [{\"attr_1\": \"val1\", \"attr_2\": \"val2\"}], \"rendundant_str_2\": [{\"attr_1\": \"val4\", \"attr_2\": \"val3\"}]}";
  2. ObjectMapper objectMapper = new ObjectMapper();
  3. List<MyObj> myObjList = new ArrayList<>();
  4. ObjectNode objectNode = (ObjectNode) objectMapper.readTree(json);
  5. for (Iterator<JsonNode> it = objectNode.elements(); it.hasNext(); ) {
  6. ArrayNode arrayNode = (ArrayNode) it.next();
  7. MyObj myObj = objectMapper.treeToValue(arrayNode.get(0), MyObj.class);
  8. myObjList.add(myObj);
  9. }
  10. System.out.println("List : " + myObjList);

输出:

  1. List : [MyObj{attr_1='val1', attr_2='val2'}, MyObj{attr_1='val4', attr_2='val3'}]

希望这有助于您的需要!

英文:
  1. String json = &quot;{\&quot;redundant_str_1\&quot;: [{\&quot;attr_1\&quot;: \&quot;val1\&quot;, \&quot;attr_2\&quot;: \&quot;val2\&quot;}], \&quot;rendundant_str_2\&quot;: [{\&quot;attr_1\&quot;: \&quot;val4\&quot;, \&quot;attr_2\&quot;: \&quot;val3\&quot;}]}&quot;;
  2. ObjectMapper objectMapper = new ObjectMapper();
  3. List&lt;MyObj&gt; myObjList = new ArrayList&lt;&gt;();
  4. ObjectNode objectNode = (ObjectNode) objectMapper.readTree(json);
  5. for (Iterator&lt;JsonNode&gt; it = objectNode.elements(); it.hasNext(); ) {
  6. ArrayNode arrayNode = (ArrayNode) it.next();
  7. MyObj myObj = objectMapper.treeToValue(arrayNode.get(0),MyObj.class);
  8. myObjList.add(myObj);
  9. }
  10. System.out.println(&quot;List : &quot; + myObjList);

Output :

  1. List : [MyObj{attr_1=&#39;val1&#39;, attr_2=&#39;val2&#39;}, MyObj{attr_1=&#39;val4&#39;,attr_2=&#39;val3&#39;}]

答案2

得分: 0

你可以使用Jackson(添加jackson-corejackson-databind的依赖项):

  1. String json = "["
  2. + "{\"attr_1\": \"val1\", \"attr_2\": \"val2\"}, "
  3. + "{\"attr_1\": \"val4\", \"attr_2\": \"val3\"}"
  4. + "]";
  5. ObjectMapper mapper = new ObjectMapper();
  6. mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
  7. List<MyObj> list = Arrays.asList(mapper.readValue(json, MyObj[].class));

或者,如果在MyObj上添加字段的setter方法或使它们成为public,那么不需要以下行:

  1. mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
英文:

You can use Jackson (add dependencies to jackson-core and jackson-databind):

  1. String json = &quot;[&quot; +
  2. &quot;{\&quot;attr_1\&quot;: \&quot;val1\&quot;, \&quot;attr_2\&quot;: \&quot;val2\&quot;}, &quot; +
  3. &quot;{\&quot;attr_1\&quot;: \&quot;val4\&quot;, \&quot;attr_2\&quot;: \&quot;val3\&quot;}&quot;
  4. + &quot;]&quot;;
  5. ObjectMapper mapper = new ObjectMapper();
  6. mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
  7. List&lt;MyObj&gt; list = Arrays.asList(mapper.readValue(json, MyObj[].class));

Or, if you add to MyObj setters on fields or make them public there will be no needed the row:

  1. mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

答案3

得分: 0

你的回应是 Map<String, List<MyObj>> 的格式。你可以将其反序列化为给定类型,然后将其转换为 List<MyObj>

  1. import com.fasterxml.jackson.core.type.TypeReference;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.fasterxml.jackson.databind.json.JsonMapper;
  4. import lombok.Data;
  5. import lombok.ToString;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.util.Collection;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.stream.Collectors;
  12. public class MyObjApp {
  13. public static void main(String[] args) throws IOException {
  14. File jsonFile = new File("./resource/test.json").getAbsoluteFile();
  15. ObjectMapper mapper = JsonMapper.builder().build();
  16. TypeReference<Map<String, List<MyObj>>> respType = new TypeReference<Map<String, List<MyObj>>>() {};
  17. Map<String, List<MyObj>> response = mapper.readValue(jsonFile, respType);
  18. List<MyObj> myObjs = response.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
  19. System.out.println(myObjs);
  20. }
  21. }
  22. @Data
  23. @ToString
  24. class MyObj {
  25. private String attr_1;
  26. private String attr_2;
  27. }

以上代码会打印出:

  1. [MyObj(attr_1=val1, attr_2=val2), MyObj(attr_1=val4, attr_2=val3)]
英文:

Your response is of form Map&lt;String, List&lt;MyObj&gt;&gt;. You can deserialise it to given type and after this convert to a List&lt;MyObj&gt;.

  1. import com.fasterxml.jackson.core.type.TypeReference;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.fasterxml.jackson.databind.json.JsonMapper;
  4. import lombok.Data;
  5. import lombok.ToString;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.util.Collection;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.stream.Collectors;
  12. public class MyObjApp {
  13. public static void main(String[] args) throws IOException {
  14. File jsonFile = new File(&quot;./resource/test.json&quot;).getAbsoluteFile();
  15. ObjectMapper mapper = JsonMapper.builder().build();
  16. TypeReference&lt;Map&lt;String, List&lt;MyObj&gt;&gt;&gt; respType = new TypeReference&lt;Map&lt;String, List&lt;MyObj&gt;&gt;&gt;() {};
  17. Map&lt;String, List&lt;MyObj&gt;&gt; response = mapper.readValue(jsonFile, respType);
  18. List&lt;MyObj&gt; myObjs = response.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
  19. System.out.println(myObjs);
  20. }
  21. }
  22. @Data
  23. @ToString
  24. class MyObj {
  25. private String attr_1;
  26. private String attr_2;
  27. }

Above code prints:

  1. [MyObj(attr_1=val1, attr_2=val2), MyObj(attr_1=val4, attr_2=val3)]

huangapple
  • 本文由 发表于 2020年7月28日 19:42:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63133322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定